2017-06-19 62 views
1

在这里我创建两个ts文件一个是服务另一个是一般服务我实现了一些服务和调用一般ts文件,而我的页面获取加载其通过错误为在NoProviderError.BaseError [作为构造]错误:无法解析CustomerService的所有参数:(?)

Service.ts

@Component({ 
    templateUrl: "../../template/customer/customer.html", 
    providers: [CustomerService] 
}) 
    Url = "http://localhost:54873/Api/Home/GetEmp" 
     public constructor(private _http: Http) { 
     } 
     getEmpData() { 
      debugger; 
      return this._http.get(this.Url).map(this.extractData).catch(this.handleError); 
     } 

Component.ts

@Component({ 
    templateUrl: "../../template/customer/customer.html", 
    providers: [CustomerService] 
}) 
@Injectable() 
    export class CustomerComponent { 
     Url = "http://localhost:54873/Api/Home/GetEmp" 
     getfun: string; 
     constructor(private _HttpService: CustomerService) { } 
     getData() { 
      return this._HttpService.getEmpData().subscribe(data => this.getfun = JSON.stringify(data), error => alert('This is error...'), 
       () => console.log()); 
     } 
+0

这是哪里的服务?您只发布了该课程的一部分。你有没有在'providers'中指定它?这个问题缺乏http://stackoverflow.com/help/mcve – estus

+0

@estus我将编辑我的代码,我指定在提供商bt其通过一个错误 –

+0

请验证我编辑的代码 –

回答

1

你不能有一个service.ts文件中@Component,它应该是如下,

@Injectable() 
export class CustomerService { 
    public constructor(private _http: Http) { 
    } 
    getEmpData(): Observable<Employee[]> { 
     debugger; 
     return this._http.get(this.Url).map(this.extractData).catch(this.handleError); 

    } 
    private extractData(res: Response) { 
     let body = res.json(); 
     // this.Employees = res.json 
     return body.data || {}; 
    } 
    private handleError(error: Response | any) { 
     // In a real world app, you might use a remote logging infrastructure 
     let errMsg: string; 
     if (error instanceof Response) { 
      const body = error.json() || ''; 
      const err = body.error || JSON.stringify(body); 
      errMsg = `${error.status} - ${error.statusText || ''} ${err}`; 
     } else { 
      errMsg = error.message ? error.message : error.toString(); 
     } 
     console.error(errMsg); 
     return Observable.throw(errMsg); 
    } 
} 
相关问题