2016-11-22 69 views
-1

只是在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> 

知道的任何幫助..

+0

是文件'Billing.Service路徑。 ts'或'billing.service.ts'? – Puigcerber

+0

@Puigcerber Billing.Service.ts –

+0

但是你的模塊是從''。/ billing.service''輸入的,還是那個輸入錯誤? – Puigcerber

回答

1

您必須添加提供商:[BillingService有]

@Component({ 
    selector: 'billing', 
    templateUrl: 'app/Billing/billing.component.html', 
    providers: [ BillingService]  
}) 

希望它將爲你工作。

+0

你能評論一下你爲什麼投票失望嗎?這將有所幫助。謝謝 –

+0

謝謝你的答案。也在那裏添加了提供者。但這似乎也沒有工作.. –

+0

爲什麼他需要添加'BillingService'到組件的提供者時,他已經在模塊中這樣做了? – Puigcerber

0

仔細檢查每個目錄路徑,錯誤說:inline template:0:0,所以如果可能的話請提供html模板代碼。

Billing.module.ts路線BillingComponent

{ path: 'billpackages', component: BillingComponent }

應該是:

{ path: '', component: BillingComponent }

如果您分配了父組件

+0

謝謝你的答案..模板html也添加了 –

相關問題