我创建了这样一个类
@Injectable FooService {
constructor(protected _bar:BarService){
}
}并像这样扩展了它
@Injectable ExtFooService extends FooService {
constructor(_bar:BarService){
super(_bar);
}
}但是后来我像这样扩展了BarService
@Injectable ExtBarService extends BarService {
public myBarVar = "myBarVar";
constructor(){
super();
}
}现在在这里
@Injectable ExtFooService extends FooService {
constructor(_bar:ExtBarService){
super(_bar);
}
someMethod(){
return {
key: this._bar.myBarVar
};
}
}但我得到了以下错误:
错误:(51,28) 28 2551:属性'myBarVar‘在'BarService’类型上不存在。
就像ExtBarService被迫去BarService是因为超级阶级想要那样.我有可能要这么做吗?
@Injectable ExtFooService extends FooService {
constructor(_bar:BarService, private _extBar: ExtBarService){
super(_bar);
}
someMethod(){
return {
key: this._extBar.myBarVar
};
}
}发布于 2018-03-16 10:33:04
您不能覆盖private字段,但无论如何不能从派生类访问该字段,因此我不知道您从哪里得到错误,但我猜这不是您的完整代码。
最简单的解决方案是使字段protected。它将无法从外部访问,它将是可访问的表单派生类,并且您可以使用派生服务覆盖该类型,而不会出现错误:
@Injectable class FooService {
constructor(protected _bar:BarService){
}
}
@Injectable class ExtFooService extends FooService {
constructor(protected _bar:ExtBarService){
super(_bar);
}
someMethod(){
return {
key: this._bar.myBarVar
};
}
}https://stackoverflow.com/questions/49318307
复制相似问题