@ ngrx / store生产版本上的错误:“ Store”的模板编译期间发生错误

时间:2018-12-31 10:27:45

标签: angular ngrx ngrx-store

错误:

ERROR in ../@ngrx/store/store.ts(10,2): Error during template compile of 'Store'
Could not resolve @angular/core relative to /home/teebo/Development/node_modules/@ngrx/store/store.d.ts..

我使用Angular v6,并且使用的是@ ngrx / store 6.1.0。

我正在从 ./ reducers / ui / index.ts 导出一些减速器,如

export const uiReducers = combineReducers({
  formFields: fromFormFieldsReducer.reducer,
  forms: fromFormReducers.reducer,
  formGroups: fromFormGroupReducer.reducer
});

然后在 appState.reducers.ts 中,我的导出如下:

import { ActionReducerMap } from '@ngrx/store';
import { uiReducers } from './reducers/ui';
import { UIState } from './models/ui.model';
import { InjectionToken } from '@angular/core';

export interface AppState {
  ui: UIState;
}

export const reducerToken = new 
 InjectionToken<ActionReducerMap<AppState>>('Reducers');

 export function getReducers() {
   return { ui: uiReducers };
 }

 export const reducerProvider = [
   { provide: reducerToken, useFactory: getReducers }
 ];

然后在我的 app.module.ts 中,我有以下内容

...
import { StoreModule, MetaReducer } from '@ngrx/store';
import { reducerToken, reducerProvider } from 
 './state_store/appState.reducers'; 
...
imports: [..., StoreModule.forRoot(reducerToken),...]
...
providers: [..., reducerProvider, ...]                             

但是运行以下npm脚本

"build:ssr": "npm run build:client-and-server-bundles && npm run 
 webpack:server",

我得到了错误

ERROR in ../@ngrx/store/store.ts(10,2): Error during template compile of 'Store'
Could not resolve @angular/core relative to /home/teebo/Development/node_modules/@ngrx/store/store.d.ts..

在此问题上的任何帮助将不胜感激,谢谢。  我已经按照我在github上的线程来跟踪类似one

的问题

在没有提供者的情况下使用ActionReducerMap进行操作

ERROR in app/app.module.ts(64,25): Error during template compile of 
'AppModule'
 Function calls are not supported in decorators but 'combineReducers' 
 was called in 'appStateReducers'
 'appStateReducers' references 'uiReducers' at 
 app/state_store/appState.reducers.ts(9,67)
  'uiReducers' calls 'combineReducers' at 
app/state_store/reducers/ui/index.ts(8,27).

2 个答案:

答案 0 :(得分:1)

请勿手动调用CombineReducers函数。如here所述,您可以尝试将actionReducerMap用于减速器并将其传递给StoreModule.forRoot(reducers)

有关实现,请参见此link

答案 1 :(得分:0)

我找到了解决该问题的方法,方法是在tsconfig.app.json中指定node_modules的路径,现在可以使用--prod进行构建。我在编译器选项中所做的更改如下

"compilerOptions": {
  "baseUrl": ".",
  "paths": { "@angular/*": ["../node_modules/@angular/*"] },
 }