使用 Injector angular 7 將 Store 注入子類 (Injecting Store to sub classes using Injector angular 7)


問題描述

使用 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 Katztimdeschryver)

參考文件

  1. Injecting Store to sub classes using Injector angular 7 (CC BY‑SA 2.5/3.0/4.0)

#angular7 #store #ngrx






相關問題

更改角度 7 中打印值的顏色 (Change the colour of printed value in angular 7)

在Angular7中更改路線的頁面重新加載問題 (Page reload issue on changing the route in Angular7)

如何使用訂閱來映射對象 (How to map object using subscribe in angular)

Angular 文件夾結構 (Angular Folder Structure)

在兄弟組件之間傳遞數據 (Passing Data between sibling components)

如何在 *ngFor 中循環選擇標籤並獲取所選值 (how to loop throught select tags inside *ngFor and get the selected value)

如何測試角度事件監聽器? (How to test angular event listeners?)

Angular 7/8 響應開發服務器上的健康檢查 (Angular 7/8 respond to health-checks on the dev server)

NgRx Store:如何在兩個功能模塊之間共享數據 (NgRx Store : How to share the data between two feature modules)

根據角度表單構建器/組中其他字段的值運行並設置一個字段的驗證錯誤 (Run and set the validation errors on one field based on the value of other field in angular form builder/group)

如何更改 Angular Material 中的標籤標籤? (How to change tab label in Angular Material?)

429 Too Many Requests - Angular 7 - 多個文件上傳 (429 Too Many Requests - Angular 7 - on multiple file upload)







留言討論