
[英]error NG2003: No suitable injection token for parameter 'id' of class
[英]No suitable injection token for parameter 'basePath' of class 'HttpService'
我正在研究 angular 项目。
我尝试了多个 inheritance。
@Injectable({
providedIn: 'root'
})
export class StoreStaffDesignationService extends StoredDataService<IStoreStaffDesignation> {
constructor(
public override http: HttpClient,
) {
super('staff-designation', http);
}
}
@Injectable({
providedIn: 'root'
})
export class StoredDataService<T> extends HttpService<T> {
constructor(public subPath: string, public override http: HttpClient) {
super('store' + '/' + subPath, http);
this.subPath = subPath;
}
}
@Injectable({ providedIn: 'root' })
export class HttpService<T> {
protected baseUrl: string;
protected basePath: string;
http: HttpClient;
constructor(basePath: string, http: HttpClient) {
this.baseUrl = `${environment.BASE_URL}`;
this.basePath = basePath;
this.http = http;
}
}
我得到了遵守失败
错误 NG2003:class 'StoredDataService' 的参数 'subPath' 没有合适的注入令牌。 考虑使用 @Inject 装饰器来指定注入令牌。
NG2003:没有适合 class 'HttpService' 的参数 'basePath' 的注入令牌。 考虑使用 @Inject 装饰器来指定注入令牌。
我的代码有什么错误?
请任何人帮助我。
错误意味着 angular 不知道如何解析此变量的实例,因为它未在 DI 容器或提供程序中注册
要解决您需要创建 InjectionToken 的问题
export const stringInjector = new InjectionToken<any>('stringInjector');
并在构造函数中使用新的注入器令牌
constructor(@Inject(stringInjector) public subPath: string, public override http: HttpClient) {
}
在此处查看链接以获取更多详细信息https://angular.io/errors/NG2003
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.