怪异的行为正如标题所说,我现在面临一个问题,我的所有效果只有在这些情况下正常工作:我已经登录 NGRX - 上影响
- m没有登录
如果我设法注销并与其他用户登录,则会出现问题。只有每个ngOnInit
做动作调度才会被调用。没有效果被解雇。
我有两个模块,app.module和pages.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的影响,他们的服务导致相同的行为。
我在做什么错?
不是你的问题的解决方案,但你有没有考虑将ngrx/store更新到版本4? –
嗨@Daniel B,是的,但我有一些问题必然会出现在角度版本上。将角度5出来时更新它。 – AndreaM16
好的,我会看看我是否可以重现您的问题! –