加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – ngrx – 对效果的奇怪行为

发布时间:2020-12-17 18:06:40 所属栏目:安全 来源:网络整理
导读:正如标题中所述,我面临的问题是,只有在这些情况下,我的所有效果才能正常工作: 我已经登录了 我没有登录 如果我设法注销并与其他用户登录,则会出现问题.只有每个ngOnInitdoing动作调度都会被调用.没有效果被解雇. 我有两个模块,app.module和pages.module.第
正如标题中所述,我面临的问题是,只有在这些情况下,我的所有效果才能正常工作:

>我已经登录了
>我没有登录

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

我有两个模块,app.module和pages.module.第一个只声明未登录用户的组件,例如login / register / reset.第二个仅声明登录用户的组件.

在app.module.ts中,我导入了所有的reducers并将其设置为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.reducer.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等相关问题进行了掠夺,但我真的无法解决这个问题.

I’m guessing it may be caused by the fact that I have these two
different modules

编辑:

在app.module中移动效果及其服务会导致相同的行为.

我究竟做错了什么?

解决方法

它出来的问题是由旧版本的ngrx引起的.我设法在 migration guide之后将所有内容升级到版本4.1.1以及README中的不同指令.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读