2016-12-01 119 views
3

我在當前應用程序中成功使用了@ ngrx/store(也是@ ngrx/effects和@ ngrx/store-devtools)。現在我想創建模塊,這將是獨立於其他應用程序的理想選擇。問題是如何在其中使用@ ngrx/store? 我能以某種方式將新的減速器添加到現有的「應用程序」商店嗎?我想避免將模型從模塊移動到應用程序,並將減速器註冊到應用程序中。有沒有人有這個解決方案?示例代碼如下:
將@ ngrx/store存儲到角度2模塊中

// App declarations 
export const APP_IMPORTS = [ 
    . 
    . 
    . 
    StoreModule.provideStore(reducer), 
    EffectsModule.run(someEffects), 
    STORE_DEV_TOOLS_IMPORTS 
]; 

@NgModule({ 
    declarations: [ 
    APP_DECLARATIONS, 
    AppComponent 
    ], 
    imports: [ 
    APP_IMPORTS 
    ], 
    providers: [APP_PROVIDERS], 
    bootstrap: [AppComponent] 
}) 
export class AppModule { 
} 

而且在新的模塊:

// Module declaration 
@NgModule({ 
    imports: [CommonModule, 
      FormsModule, 
      StoreModule.provideStore({ counter: counterReducer }) // <-- how to change this to just add to current store new reducer?? 
    ], 
    exports: [MyTestComponent], 
    declarations: [MyTestComponent], 
}) 
export class SomeModule { 
} 

也是劑量人知道如何chnage的名字@ NGRX /存儲上devtool顯示?把它改成ngrx-store-some_random_number到一些app_name?

非常感謝

+0

有rpthat涉及到當前打開的問題這個:https://github.com/ngrx/store/issues/281和https://github.com/ngrx/store/issues/211還有一個提議,但還沒有實施的變化:https:// gist .github.com/MikeRyan52/5d361681ed0c81e38775dd2db15ae202 – cartant

+0

這個我們將是一件很棒的事。我們可以更改當前商店的名稱嗎? – CoYoTe

回答

2

由於ngrx4您提供專賣店爲:StoreModule.forRoot({router: routerReducer})

@NgModule({ 
    imports: [ 
     StoreModule.forRoot({router: routerReducer}), 
     EffectsModule.forRoot([...]), 
     ... 
    ], 
    declarations: [], 
    ... 
}) 
export class AppModule {} 

那麼你的功能模塊,可以提供儲存爲StoreModule.forFeature(...)

@NgModule({ 
    imports: [ 
     StoreModule.forFeature('feature', featureReducer), 
     EffectsModule.forFeature([...]), 
     ... 
    ], 
    declarations: [], 
    ... 
}) 
export class FeatureModule {} 

feature狀態會然後根據state.feature存儲中的密鑰。

使用可以使用選擇訪問一貫特色店:

export const selectFeatureModule = createFeatureSelector<FeatureState>('feature'); 
export const selectFeatureValue = createSelector(selectFeatureModule , 
(state: FeatureState) => state.someValue); 

在功能模塊:角狀的部件可以使用/切片狀態:

... 
constructor(private store: Store<AppState>,) { 
    this.store.select(selectFeatureValue) 
     .subscribe(console.log.bind(console)); 

} 
...