Typescript Angular2没有服务错误的提供程序
从中,我抓取了代码并将其稍微修改为:Typescript Angular2没有服务错误的提供程序,typescript,angular,Typescript,Angular,从中,我抓取了代码并将其稍微修改为: @Injectable() export class ApiService { constructor(public http: Http) {} get(url: string) { return http.get(url); } } @Injectable() export abstract class BaseService { constructor(protected serv: ApiService) {} }
@Injectable()
export class ApiService {
constructor(public http: Http) {}
get(url: string) {
return http.get(url);
}
}
@Injectable()
export abstract class BaseService {
constructor(protected serv: ApiService) {}
}
@Injectable()
export class MediaService extends BaseService {
// Why do we need this?
constructor(s: ApiService) {
super(s)
}
makeCall() {
this.serv.get("http://www.example.com/get_data")
}
}
但是,当我尝试运行此代码时,控制台中出现错误:
NoProviderError{消息:“没有ApiService的提供商!(应用->
MediaService->ApiService)”,堆栈:“错误:DI异常↵
我已经创建了一个Plunkr,代码如下:
有人知道导致此错误的原因吗?您还需要在
providers
属性中定义ApiService
@Component({
selector: 'my-app',
providers: [MediaService, ApiService], // <-----
template: `
<div>
<h2>Hello {{name}}</h2>
</div>
`,
directives: []
})
export class App {
constructor(mediaService: MediaService) {
this.name = 'Angular2'
console.log('start');
mediaService.makeCall();
}
}
@组件({
选择器:“我的应用程序”,
提供者:[媒体服务,ApiService]//