【发布时间】:2020-06-12 04:32:28
【问题描述】:
有没有办法从抽象服务调用子类方法?当我这样做时,if 语句不会执行,因为onInit 不存在。我不确定为什么它不存在。也许有一种“角度”的方式来做到这一点,而不是在初始化时触发它。或者,也许我只需要在组件中手动调用 onInit。基本上我正在做的是尝试从服务器获取初始应用程序数据。
@Injectable({providedIn:'root'})
export class RootService {
public constructor(httpClient: HttpClient) {
if(typeof this.onInit === 'function') {
this.onInit()
}
}
}
@Injectable({providedIn:'root'})
export class MyService extends RootService {
public onInit() {
// Do stuff
}
}
@Component()
export MyComponent {
public constructor(myService: MyService) {}
}
【问题讨论】:
-
RootService怎么知道MyService甚至存在?你想完成什么,也许还有其他方法
标签: javascript angular typescript