我有一个使用ngrx的angular4应用程序。我创建了一个undo高阶reducer,如on the redux website所述。
不幸的是,当我在root.reducers.ts文件中应用此函数时,我遇到了提前(aot)编译的问题,并得到了此错误
ERROR in Error encountered resolving symbol values statically. Calling function 'undoable', function calls are not supported. Consider replacing the function or lambda with a reference to an exported function, resolving symbol rootReducer
当aot被关闭或文件被修改后,代码工作得很好,在这种情况下,使用的是即时编译。
我以前试过使用ngrx-undo,但似乎不起作用,这就是我推出自己解决方案的原因。
我试过查看其他stackoverflow帖子并搜索google,但大多数其他问题似乎与第三方库或自定义组件有关,这与本例无关。
我知道我可以通过删除高阶的reducer并将cartdreducer直接转换为可撤销的来解决这个问题,但这并不可取,因为如果我们使其他任何reducer都可撤销,我们将有大量的代码重复。
如何解决此错误?最好不要删除aot编译,因为一旦我们投入生产就需要这样做。
代码:
See Gist for complete code
根异径管

import { sessionReducer } from './reducers/session.reducers';
import { cartReducer } from './reducers/cart.reducer';
import { undoable } from './reducers/undo.reducer';
import { paymentReducer } from './reducers/payment.reducer';
import { accountReducer } from './reducers/account.reducer';
import { productReducer } from './reducers/product.reducer';
import { errorReducer } from './reducers/error.reducer';

export const rootReducer = {
  session: sessionReducer,
  cart: undoable(cartReducer),
  payment: paymentReducer,
  account: accountReducer,
  product: productReducer,
  error: errorReducer
};

最佳答案

最后,我将大部分撤销还原器代码抽象成单独的函数,并返回一个构建特定还原器的函数。这似乎解决了问题
大车减速器内

const initialState: UndoState<CartState> = buildInitialState(initialCartState);
export function undoableCartReducer(state = initialState, action): UndoState<CartState> {
    return handleAction(state, action, cartReducer);
}

在undo.reducer.ts中
export function buildInitialState<S>(state: S): UndoState<S> {
    return {
        past: [],
        present: state,
        future: []
    };
}

export function delegate<S>(state: UndoState<S>, action, reducer: ActionReducer<S>): UndoState<S> {
    const {past, present, future} = state;
    const newPresent = reducer(present, action);
    if (present === newPresent) {
       return state;
    }
    return {
        past: [...past, present],
        present: newPresent,
        future: []
    };
}

export function handleAction<S>(state: UndoState<S>, action, reducer: ActionReducer<S>): UndoState<S> {
    switch (action.type) {
        case 'UNDO':
            return undo(state);
        case 'REDO':
            return redo(state);
        default:
            return delegate(state, action, reducer);
    }
}

新根还原剂
export const rootReducer = {
  session: sessionReducer,
  cart: undoableCartReducer,
  payment: paymentReducer,
  account: accountReducer,
  product: productReducer,
  error: errorReducer
};

关于angular - 静态解析符号时遇到错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45357931/

10-09 23:44