我正在做一个有角度的项目。
我尝试了多重继承。
@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:类"StoredDataService"的参数"subPath"没有合适的注入令牌。考虑使用@Inject装饰器来指定注入令牌。
NG2003:类"HttpService"的参数"basePath"没有合适的注入令牌。考虑使用@Inject装饰器来指定注入令牌。
我的代码中有什么错误?
请任何人帮我。
错误平均角度不知道如何解决此变量的实例,因为它没有在DI Container或Provider 中注册
要解决问题,您需要创建InjectionToken
export const stringInjector = new InjectionToken<any>('stringInjector');
并在contractor 中使用新的注入器令牌
constructor(@Inject(stringInjector) public subPath: string, public override http: HttpClient) {
}
查看此处的链接以了解更多详细信息https://angular.io/errors/NG2003