" TypeError:无法读取属性'发布'未定义"在ngrx商店中,仅在生产版本中使用角度

时间:2018-01-16 09:36:02

标签: angular typescript ngrx ngrx-store

我有一个问题,但只是在运行时生成构建之后。现在我不确定这是一个错误还是我做错了。 我收到了错误" " TypeError:无法读取属性'发布'未定义"。"在控制台(浏览器)中,如果我使用ngrx加载以下功能模块(在运行时):

    import { AccountDto } from '../../../dto';
import * as fromAccountActions from '../actions/account.actions';

export interface AccountState {
  loading: boolean;
  loaded: boolean;
accountItems: AccountDto[];
}

export const initialState: AccountState = {
  loading: false,
  loaded: false,
  accountItems: []
};


export function accountReducer(
  state = initialState,
  action: fromAccountActions.AccountActions
): AccountState {

  switch (action.type) {
    case fromAccountActions.LOAD_ACCOUNT: {
      // console.log(action.type);
      return { ...state, loading: true };
    }

    case fromAccountActions.LOAD_ACCOUNT_FINISHED: {
      console.log('Finished: ' + action.payload);
      return {
        ...state,
        loading: false,
        loaded: true,
        accountItems: action.payload
      };
    }

    case fromAccountActions.LOAD_ACCOUNT_FAILED: {
      return {
        ...state,
        loading: false,
        loaded: false,
        accountItems: []
      };
    }

    default:
      return state;
  }
}

export const getAccountItems = (state: AccountState) => state.accountItems;
export const getAccountLoading = (state: AccountState) => state.loading;
export const getAccountLoaded = (state: AccountState) => state.loaded;

减压器中的index.ts

import * as fromReducer from './account.reducers';
import { ActionReducerMap } from '@ngrx/store';
import { createFeatureSelector } from '@ngrx/store';

export interface AccountState {
  account: fromReducer.AccountState;
}

export const reducers: ActionReducerMap<AccountState> = {
  account: fromReducer.accountReducer
};

export const getAccountState = createFeatureSelector<AccountState>('account');

account.selectors.ts

import { createSelector } from '@ngrx/store';
import * as fromReducer from '../reducers/account.reducers';
import * as fromFeature from '../reducers';

export const getCompleteAccountState = createSelector(
  fromFeature.getAccountState,
  (state: fromFeature.AccountState) => state.account
);
export const getAccountLoading = createSelector(
  this.getCompleteAccountState,
  fromReducer.getAccountLoading
);
export const getAccountLoaded = createSelector(
  this.getCompleteAccountState,
  fromReducer.getAccountLoaded
);
export const getAllAccountItems = createSelector(
  this.getCompleteAccountState,
  fromReducer.getAccountItems
);

account.module.ts

import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { TranslateModule } from '@ngx-translate/core';

import { ThirdPartyModule } from '../thirdParty/thirdParty.module';
import { SharedModule } from './../shared/shared.module';
import { AccountListComponent } from './account-list/account-list.component';
import { AccountRoutesModule } from './account.routes';
import { StoreModule } from '@ngrx/store';
import { EffectsModule } from '@ngrx/effects';
import { reducers, AccountEffects } from 'app/+account/store';

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    SharedModule,
    AccountRoutesModule,
    ThirdPartyModule,
    TranslateModule,
    StoreModule.forFeature('account', reducers),
    EffectsModule.forFeature([AccountEffects])
  ],

  declarations: [AccountListComponent],
  providers: [],
  exports: []
})
export class AccountModule {}

非常感谢帮助。 感谢

2 个答案:

答案 0 :(得分:0)

我明白了。 &#34;这&#34;在这种情况下,陈述是错误的。

import { createSelector } from '@ngrx/store';
import * as fromReducer from '../reducers/account.reducers';
import * as fromFeature from '../reducers';

export const getCompleteAccountState = createSelector(
  fromFeature.getAccountState,
  (state: fromFeature.AccountState) => state.account
);
export const getAccountLoading = createSelector(
  getCompleteAccountState,
  fromReducer.getAccountLoading
);
export const getAccountLoaded = createSelector(
  getCompleteAccountState,
  fromReducer.getAccountLoaded
);
export const getAllAccountItems = createSelector(
  getCompleteAccountState,
  fromReducer.getAccountItems
)

的工作原理。谢谢大家。

干杯

答案 1 :(得分:0)

从桶文件中导入文件时,使用NgRx时遇到同样的错误。

当您处于状态功能内时,如果位于一个从桶index.ts文件导入其中一个导出文件的文件中,则可能会出现此错误。在特征文件夹中进行导入时,应该从相对路径进行导入。只有功能文件夹之外的文件才应使用桶形文件导入内容。

我没有明确进行导入。我的IDE是自动完成的。花了半天时间才弄清楚。