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]
Comments
Post a Comment