1

这是我在小型应用程序中用于状态的 Angular 2 代码(感谢 Victor Savkin 的文章):

const initState = new OpaqueToken("initState");
export const dispatcher = new OpaqueToken("dispatcher");
export const state = new OpaqueToken("state");

const dispValue = new Subject<Action>();
const initStateValue = { subscribers: [], news: [] };

export const stateAndDispatcher = [
    {
        provide: initState,
        useValue: initStateValue
    },
    {
        provide: dispatcher,
        useValue: dispValue
    },
    {
        provide: state,
        useFactory: stateFn,
        deps: [initState, dispatcher]
    }
];

在我用 AOT 编译它之前,这一切都有效。我的回购基于@qdouble 伟大的回购 - https://github.com/qdouble/angular-webpack2-starter/

AOT 编译抛出此错误:

静态解析符号值时遇到错误。不支持函数调用。考虑将函数或 lambda 替换为对导出函数的引用,解析符号 stateAndDispatcher

有人可以帮忙解决这个问题吗?在我的情况下,我不明白这个错误的原因。

更新- 这是 stateFn (从另一个文件导入):

export function stateFn(initState: AppState, actions: Observable<Action>): Observable<AppState> {
    const appStateObs: Observable<AppState> = subscribers(initState.subscribers, actions)
        .zip(processNews(initState.news, actions))
        .map(s => ({ subscribers: s[0], news: s[1] }));
    return wrapIntoBehavior(initState, appStateObs);
}
4

1 回答 1

3

用于提供程序的所有方法都需要导出并且必须是“常规”函数。map因此,您需要将箭头函数更改为导出的常规函数​​+wrapIntoBehaviour需要导出。我不确定您的processNews方法看起来如何,但也必须导出。

于 2016-12-07T01:06:52.820 回答