20
从Angular2 access global service without including it in every constructor我已经抓住了代码,并略作修改成:Angular2无提供服务错误
@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 {message: "No provider for ApiService! (App -> MediaService -> ApiService)", stack: "Error: DI Exception↵
我在https://plnkr.co/edit/We2k9PDsYMVQWguMhhGl
创建Plunkr与代码有谁知道是什么原因导致这个错误?