angular - How to use service class in angular2 -
i new in both angular2 , .net core followed this tutorial , learned pretty solution work angular (and .netcore). have problem!
when create service this:
import { injectable } '@angular/core' import { http } '@angular/http' import 'rxjs/rx' import { accountsummary } './account-summary.type' import { accountdetail } './account-detail.type' import { accounttype } './account-type.enum' @injectable() export class accountservice { constructor(private http : http) { } getaccountsummeries() { debugger return this.http.get('api/bank/getaccountsummeries') .map(response => response.json() accountsummary[]) .topromise(); } } and import in app.module.share.ts file:
. . import { accountservice } './components/shared/account.service' //here<< export const sharedconfig: ngmodule = { bootstrap: [appcomponent], //namespace off components: declarations: [ appcomponent, navmenucomponent, countercomponent, fetchdatacomponent, homecomponent, accountlistcomponent, accountsummarycomponent, examplecomponent, formataccountnumberpipe, headercomponent ], imports: [ routermodule.forroot([ //paths... ]) ], providers: [accountservice] //here<<<<<<<<<<<<<<<<<<<<< }; iv got error when ran application:
exception: call node module failed error: error: uncaught (in promise): error: no provider accountservice! error: no provider accountservice! @ error (native)
i use service this:
import { component } '@angular/core'; import { accountsummary } '../../shared/account-summary.type' import { accounttype } '../../shared/account-type.enum' import { accountservice } '../../shared/account.service' @component({ selector: 'account-list', templateurl: './account-list.component.html' }) export class accountlistcomponent { cashaccounts: accountsummary[]; creditaccounts: accountsummary[]; constructor(private accountservice: accountservice) { } ngoninit() { this.accountservice.getaccountsummeries().then(accounts => { this.cashaccounts = accounts }) } }
according exception, didn't add in module. add accountservice in app.module.ts file
providers: [accountservice]
The issue comes down to Angular's dependency injection configuration and where the AccountService provider is registered. The component correctly requests the service through its constructor, but Angular must be able to find that provider in the module injector used by the component.
ReplyDeleteMoving AccountService into the appropriate module's providers array resolves the No provider AccountService error, while the constructor injection can then create the service and supply it to the component. This is a useful dependency-injection concept to understand when working through an Angular Course.
The example also shows how the service uses TypeScript types, constructor injection, HTTP calls, and promise-based handling of the returned data. Understanding how classes, imports, decorators, and typed dependencies work together through TypeScript Course material can make these Angular service errors easier to diagnose. The same structure can also be useful when building Angular Projects that separate API communication from component logic.
ReplyDelete