只是在ASP MVC內核的角度2中創建應用程序。已經制作了一個組件,模塊和一項服務。但在被調用時,它似乎提高了以下錯誤:角度2錯誤沒有「BillingService」的提供者
Error: Error in ./BillingComponent class BillingComponent_Host - inline template:0:0 caused by: No provider for BillingService
的billing.component.ts文件如下:
import { Component, OnInit } from '@angular/core';
import { BillingPackage } from './BillingPackage';
import { BillingService } from './Billing.Service';
@Component({
templateUrl: 'app/Billing/billing.component.html'
})
export class BillingComponent /* implements OnInit*/ {
pageTitle: string = 'Billing Options';
listFilter: string;
errorMessage: string;
packages: BillingPackage[];
constructor(private _billingService: BillingService) {
}
ngOnInit(): void {
debugger;
this._billingService.getPackages()
.subscribe(billing => this.packages = billing,
error => this.errorMessage = <any>error);
}
}
的Billing.service.ts如下:
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import { BillingPackage } from './BillingPackage';
@Injectable()
export class BillingService {
//private _companyUrl = 'api/companies/companies.json';
private _billingUrl = 'api/BillingPackage.json';
constructor(private _http: Http) { }
getPackages(): Observable<BillingPackage[]> {
return this._http.get(this._billingUrl)
.map((response: Response) => <BillingPackage[]>response.json())
.do(data => console.log('All: ' + JSON.stringify(data)))
.catch(this.handleError);
}
getPackage(id: string): Observable<BillingPackage> {
return this.getPackages()
.map((billing: BillingPackage[]) => billing.find(p => p.Id == id));
}
private handleError(error: Response) {
console.error(error);
return Observable.throw(error.json().error || 'Server error');
}
}
和Billing.module.ts如下:
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { BillingComponent } from './billing.component';
import { BillingService } from './billing.service';
import { SharedModule } from '../shared/shared.module';
@NgModule({
imports: [
SharedModule,
RouterModule.forChild([
{ path: 'billpackages', component: BillingComponent },
])
],
declarations: [
BillingComponent
],
providers: [
BillingService,
]
})
export class BillingModule { }
的Billing.component.html文件是在:
<div class="row" *ngIf="packages && packages.length">
<div class="col-xs-12 col-sm-6 col-md-4" *ngFor="let package of packages">
<div class='panel panel-info'>
<div class='panel-heading'>
{{package.Name}}
</div>
<div class='panel-body'>
<div class='has-error' *ngIf='errorMessage'>{{errorMessage}}</div>
<div class="row">
<h2> <span class="label label-success"> {{package.AllowedUsersCount}}</span> Users</h2>
</div>
<div class="row">
<h2> <span class="label label-info"> {{package.AllowedItemCount}}</span> Products</h2>
</div>
<div class="row">
<h2> <span class="label label-warning"> {{package.AllowedTransactions}}</span> Transactions</h2>
</div>
<div class="row">
<h2> <span class="label label-danger"> {{package.AllowedStoreCount}}</span> Stores</h2>
</div>
<div class="row">
<h2> For <span class="label label-primary"> {{package.MonthlyCharges}}</span></h2>
</div>
</div>
</div>
</div>
</div>
知道的任何幫助..
是文件'Billing.Service路徑。 ts'或'billing.service.ts'? – Puigcerber
@Puigcerber Billing.Service.ts –
但是你的模塊是從''。/ billing.service''輸入的,還是那個輸入錯誤? – Puigcerber