![](/img/trans.png)
[英]Why do I get 'Next is not a function' when I try to export a Middleware function to module.exports object?
[英]Why do I have to export the function I use in angular appModule import module?
我有以下代码
@NgModule({ declarations: [ ... ], imports: [ RoutingModule, SharedModule, JwtModule.forRoot({ config: { headerName: 'Authorization', tokenGetter: () => localStorage.getItem('token'), // <———— this line has problem whitelistedDomains: ['localhost:4200'] //blacklistedRoutes: ['localhost:3001/auth/', 'foo.com/bar/'] } }) ], ... }) export class AppModule { }
它使用ng serve
,但是运行ng build --prod
时出现以下错误
ERROR in Error during template compile of 'AppModule'
Function expressions are not supported in decorators in 'ɵ0'
'ɵ0' contains the error at app/app.module.ts(36,22)
Consider changing the function expression into an exported function.
然后我将代码修改为
function getToken() { return localStorage.getItem('token') } … JwtModule.forRoot({ config: { headerName: 'Authorization', tokenGetter: () => getToken, whitelistedDomains: ['localhost:4200'] ...
而且还是不开心
ERROR in app/app.module.ts(19,10): Error during template compile of
'AppModule'
Reference to a non-exported function.
最后,我通过导出getToken
函数解决了该问题。
我有以下问题
ng serve
有效但ng build --prod
? 您遇到的问题归因于Angular中的Ahead-of-Time(AOT)编译器。 默认情况下, ng serve
和ng build
使用即时(JIT)编译器。 但是, ng build --prod
使用AOT编译器。 您可以通过ng serve --aot
模拟相同的行为。
所以,对你的问题。
希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.