2016-09-21 37 views
1

我需要能夠從NgModule進口部注入,並使用角2服務(下面稱爲SomeService)的NgModule進口部內注入依賴因爲我需要使用普通函數(上面命名爲someFunction)的功能完整的服務(取決於Http),以便重新水化ngrx商店應用程序。的角2的應用程序

這裏someFunction是一個薈萃還原劑。

在ngrx商店應用程序中查看meta reducer的概念。

有人可以幫忙嗎?

回答

1

不能完全肯定我明白你正在嘗試做的(沒有看到更多的代碼),但我想你正在嘗試做可以做到用工廠的提供者配置

providers: [ 
    { 
    provide: WhateverService, 
    useFactory: (things: Things, to: To, inject: Inject) => { 
     // Not 100% sure, but I believe the return should be 
     // synchronous. If you have some asynchronous actions 
     // to be resolved first, you may just want to pass the 
     // Promise or Observable to the constructor 
     return new WhateverService(...); 
    }, 
    deps: [Things, To, Inject] 
    } 
] 

在您StoreModule,它可以像

@NgModule({}) 
export class StoreModule { 
    static provideStore(variable) { 
    return { 
     ngModule: StoreModule, 
     providers: [ 
     { 
      provide: WhateverService, 
      useFactory: (things: Things, to: To, inject: Inject) => { 
      // use variable here 
      return new WhateverService(...); 
      }, 
      deps: [Things, To, Inject] 
     } 
     ] 
    }  
    } 
} 

另一種選擇,如果你想引導之前解決一些偏遠的數據,是做這樣的事情this。除此之外,我可能會完全關閉,因爲我對ngrx不熟悉。

+0

我使用了來自鏈接回覆http://stackoverflow.com/a/39454713/536299的示例。非常感謝。 – balteo

相關問題