問題描述
使用 Injector angular 7 將 Store 注入子類 (Injecting Store to sub classes using Injector angular 7)
我將一個超級抽像類的服務注入到我們的子類中。這工作正常,商店服務除外。我正在做的是以下內容:
超類:
export abstract class GenericClass {
translate: TranslateService;
cdr: ChangeDetectorRef;
someService: SomeService;
otherService: OtherService;
anotherService: AnotherService;
constructor(injector: Injector){
this.translate = injector.get(TranslateService);
this.cdr = injector.get(ChangeDetectorRef);
this.someService= injector.get(SomeService);
this.otherService = injector.get(OtherService);
this.anotherService= injector.get(AnotherService);
}
}
子類(組件):
export class SubClassComponent {
constructor(injector: Injector){
super(injector);
}
}
這工作正常,但存儲它沒有。當我以相同的方式將 Store 添加到超類時,例如:
Super Class:
export abstract class GenericClass {
...
store: Store<AppState>;
constructor(injector: Injector){
...
this.store = injector.get(Store<AppState>);
}
}
在這種情況下,我收到以下錯誤:
ERROR in fox‑generic‑form.ts(45,30): error TS2348: Value of type 'typeof Store' is not callable. Did you mean to include 'new'?
I按照錯誤中的建議嘗試了此操作:
this.store = injector.get(new Store<AppState>);
但是在這種情況下,我在 Store 的構造函數中收到所需參數的錯誤,並且在檢查後確實需要 3 個不同的參數:
store.d.ts:
constructor(state$: StateObservable, actionsObserver: ActionsSubject, reducerManager: ReducerManager);
我' 已經在網上搜索了一段時間,但我找不到解決方案,我確實找到了測試場景,但是對於這種組件超類 & Injector。
任何人都知道如何將Store與超類中的Injector一起使用?或者我如何在 Store 中使用這 3 個參數(state$: StateObservable、actionObserver: ActionsSubject、reducerManager: ReducerManager)?
參考解法
方法 1:
Should work if you would do injector.get(Store)
, without the generic type.
(by Tomas Katz、timdeschryver)