NG-CLi - 1.0.0-beta.28.3
Angular2 : 2.4.6
这是我在 app.module.ts 中的函数
export function authFactory(auth : LoginService){
return auth.loginUser();
}
提供者是这样的引用
{
provide: APP_INITIALIZER,
//useFactory: (auth: AuthService) => () => auth.authUser(),
useFactory : authFactory,
deps: [LoginService],
multi: true
}
这一直在运行,直到我们将 angular cli
和 Angular 2
从 2.1.0
升级到 2.4.6
.我们还将 cli 版本从 beta 18
升级到 28
。
现在,当应用程序启动时,它会在控制台中给出此错误,我确实看到该请求在 loginUser 方法中运行良好。但不知何故,它给出了有关 http post 请求中使用的 promise 的错误
Unhandled Promise rejection: appInits[i] is not a function ; Zone: ; Task: Promise.then ; Value: TypeError: appInits[i] is not a function
最佳答案
当使用的工厂函数返回undefined 时会发生此错误(至少在我的情况下)。
就我而言,我错误地翻译了:
useFactory: () => () => {}
到
useFactory: noop,
export function noop() {}
并解决它改变它:
export function noop(){}
到
export function noop() {
return () => {};
}
...工厂是一个空函数的原因是因为我有一些实体应该在 DIC 中可用而没有任何实体显式注入(inject)它们
关于使用 ng serve 时 Angular2 APP Init [i] 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42114121/