Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I have to export the function I use in angular appModule import module?

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

  1. Why ng serve works but not ng build --prod?
  2. Why inline lambda doesn't work?
  3. Why do I have to export the function?
like image 698
ycshao Avatar asked May 13 '18 01:05

ycshao


People also ask

Why do we use export in Angular?

An export what you put is the exports property of the @NgModule decorator. It enables an Angular module to expose some of its components/directives/pipes to the other modules in the applications. Without it, the components/directives/pipes defined in a module could only be used in that module.

What does export mean in Angular?

Similarly, the export keyword lets you declare variables, functions, and classes that the module should be exposed to other scripts. Using the export keyword, you can make selected APIs available to other modules. A module's functions, variables, and classes that aren't explicitly exported remain private to the module.

Which of the following can be exported from a module in Angular?

You can export any declarable class —components, directives, and pipes— whether it's declared in this NgModule or in an imported NgModule.

What is imports and exports in NgModule?

The set of components, directives, and pipes declared in this NgModule that can be used in the template of any component that is part of an NgModule that imports this NgModule. Exported declarations are the module's public API. exports?: Array<Type<any> | any[]> A declarable belongs to one and only one NgModule.


1 Answers

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.

  1. See above explanation.
  2. The AOT Collector does not support the arrow function syntax.
  3. Angular generates a class factory in a separate module and that factory can only access exported functions.

Hope this helps!

like image 88
Drewness Avatar answered Oct 16 '22 11:10

Drewness