2017-10-17 114 views
5

怪异的行为正如标题所说,我现在面临一个问题,我的所有效果只有在这些情况下正常工作:我已经登录 NGRX - 上影响

    • m没有登录

    如果我设法注销并与其他用户登录,则会出现问题。只有每个ngOnInit做动作调度才会被调用。没有效果被解雇。

    我有两个模块,app.modulepages.module。第一个声明只有组件没有登录的用户,如login/register/reset。第二个只声明登录用户的组件。

    app.module.ts我导入所有reducer并将其设置为StoreModule.provideStore(reducer)。在另一个模块pages.module.ts中,我导入并激活了诸如EffectsModule.run(TestEffects)的效果。

    使用以下代码只会调用ngOnInit内部的日志。效果内的日志永远不会被调用。

    test.component.ts

    /** ngrx **/ 
    import * as reducers from '../../reducers'; 
    import * as testActions from './actions/test.actions'; 
    
    . . . 
    
    constructor(private _store: Store<reducers.State>){ 
        this._store.select(reducers.getStuff) 
         .subscribe(stuff => console.log(stuff)); 
    } 
    
    ngOnInit() { 
        console.log('Dispatching Load Action'); 
        this._store.dispatch(new testActions.LoadAction()); 
    } 
    

    test.actions.ts

    import { Action } from '@ngrx/store'; 
    
    export const ActionTypes = { 
        LOAD: type('[Test] Load'), 
        LOAD_SUCCESS: type('[Test] Load Success'), 
    }; 
    
    export class LoadAction implements Action { 
        type = ActionTypes.LOAD; 
        constructor() {} 
    } 
    
    export class LoadActionSuccess implements Action { 
        type = ActionTypes.LOAD_SUCCESS; 
        constructor(public payload: SomeType) {} 
    } 
    
    export type Actions 
        = LoadAction 
        | LoadActionSuccess 
    

    test.effects.ts

    @Injectable() 
    export class TestEffects { 
        @Effect() load$ = this.actions$ 
        .ofType(testActions.ActionTypes.LOAD) 
        .map(toPayload) 
        .switchMap(() => this._testService.getStuff() 
         .map(result => new testActions.LoadActionSuccess(result)) 
         .catch((error) => Observable.of({type: error})) 
        ) 
        ; 
    
        constructor(private _testService: TestService, private actions$: Actions) {} 
    } 
    

    test.service.ts

    import { createSelector } from 'reselect'; 
    import { Action } from '@ngrx/store'; 
    /** ngrx **/ 
    import * as sidebarActions from '../actions/test.actions'; 
    
    export interface State { 
        stuff: SomeType 
    }; 
    
    export const initialState: State = { 
        stuff: new SomeType() 
    }; 
    
    export function testReducer(state = initialState, action: Action): State { 
        switch (action.type) { 
         case testActions.ActionTypes.LOAD_SUCCESS: 
          return { 
           stuff: new SomeType(action.payload) 
          } 
         default: { 
          return state; 
         } 
        } 
    } 
    
    export const getStuff = (state: State) => state.stuff; 
    

    在我的package.json我:

    { 
        "dependencies" : { 
         "@angular/core": "^4.0.0", 
         . . ., 
         "@ngrx/core": "^1.2.0", 
         "@ngrx/effects": "^2.0.4", 
         "@ngrx/store": "^2.2.3", 
         . . . 
        }, 
        "devDependencies" : { 
         . . . 
         "ngrx-store-freeze": "^0.1.9", 
         . . . 
        } 
    } 
    

    我真的不理解这种行为。我也采取了对ngrx github问题和相关问题如this one的战利品,但我真的不能解决这个问题。

    我猜它可能的事实,我有这两个 不同的模块引起

    编辑

    移动内部app.module的影响,他们的服务导致相同的行为。

    我在做什么错?

  • +0

    不是你的问题的解决方案,但你有没有考虑将ngrx/store更新到版本4? –

    +0

    嗨@Daniel B,是的,但我有一些问题必然会出现在角度版本上。将角度5出来时更新它。 – AndreaM16

    +1

    好的,我会看看我是否可以重现您的问题! –

    回答

    0

    它出来的问题是由旧版本ngrx引起的。我设法将所有内容升级到版本4.1.1之后的migration guide和README中的不同说明。