2016-12-01 5 views
3

現在のアプリケーションで@ ngrx/store(@ ngrx/effectsと@ ngrx/store-devtoolsも)を使用しています。今はモジュールを作って、残りのアプリケーションから独立させるのが理想的です。問題は@ ngrx/storeをそれにも使用する方法ですか? 私は何とか新しい減速機を既存の「アプリ」ストアに追加できますか?私はモデルからアプリケーションへモデルを移動し、減速機の登録をアプリにするのを避けたいと思います。誰かがこれからの解決策を持っていますか?サンプルコードは以下の通りです:
アンガー2のモジュールに@ngrx/storeを使用する

// 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 { 
} 

もdevtoolに示す@ ngrx /ストアの名前をchnageする方法を誰もが知っている線量?それをngrx-store-some_random_number形式からapp_nameに変更しますか?

あなたのように店舗を提供ngrx4の多くのおかげ

+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

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)); 

} 
... 
関連する問題