I have following code
@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 { }
It works using ng serve
, but I got following error when I run 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.
Then I modified my code to
function getToken() { return localStorage.getItem('token') } … JwtModule.forRoot({ config: { headerName: 'Authorization', tokenGetter: () => getToken, whitelistedDomains: ['localhost:4200'] ...
And it's still not happy
ERROR in app/app.module.ts(19,10): Error during template compile of
'AppModule'
Reference to a non-exported function.
At the end, I solved the issue by exporting the getToken
function.
I have following question
ng serve
works but not ng build --prod
? The issues you are having are due to the Ahead-of-Time (AOT) compiler in Angular. By default, ng serve
and ng build
use the Just-in-Time (JIT) compiler. However, ng build --prod
uses the AOT compiler. You can simulate this same behavior doing ng serve --aot
.
So, to your questions.
Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.