如何在angular2

时间:2017-07-25 14:00:31

标签: angular

我是angular2.net core的新用户我遵循了this教程并学习了使用angular(和.netcore )的漂亮解决方案。我只是有问题!

当我创建这样的服务时:

import { Injectable } from '@angular/core'
import { Http } from '@angular/http'
import 'rxjs/RX'
import { AccountSummary } from './account-summary.type'
import { AccountDetail } from './account-detail.type'
import { AccountType } from './account-type.enum'

@Injectable()

export class AccountService
{
    constructor(private http : Http)
    {
     }
getAccountSummeries()
{
    debugger
    return this.http.get('api/Bank/GetAccountSummeries')
        .map(response => response.json() as AccountSummary[])
        .toPromise();
}
}

并将其导入app.module.share.ts文件:

   .
   .

import { AccountService } from './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<<<<<<<<<<<<<<<<<<<<<

};

运行应用程序时出现此错误:

  

异常:调用节点模块失败,错误:错误:未捕获(在承诺中):错误:没有AccountService的提供者!   错误:没有AccountService提供商!   在错误(本机)

我使用我的服务:

import { Component } from '@angular/core';
import { AccountSummary } from '../../shared/account-summary.type'
import { AccountType } from '../../shared/account-type.enum'
import { AccountService } from '../../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 })
    }
}

1 个答案:

答案 0 :(得分:2)

根据例外情况,您没有添加模块。在app.module.ts文件中添加AccountService

providers: [AccountService]