Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Library which is used by another app produces error: Function calls are not supported in decorators but 'LoggerModule' was called

I wrote an angular library with some services to use this in different angular applications. Everything works fine without --prod, so without AOT compile, in the angular application.

Generating the library with ng-packagr as well as with the cli as well as with some different yeoman generators produces everytime the same error. Besides I tried different ng-versions (5.x.x, 6.x.x & 7.x.x). But in all cases everytime (with AOT) the same error when I call LoggerModule.forRoot() in the app.module of the application:

ERROR in Error during template compile of 'AppModule' 
Function calls are not supported in decorators but 'LoggerModule' was called.

I read many articles about this topic, tried different angularCompilerOptions in tsconfig. Any further ideas out there? The module works fine without AOT (but this is no option for us)...

NgModule of the library:

@NgModule({
  declarations: [],
  imports: [],
  providers: []
})
export class LoggerModule {

  static forRoot(): ModuleWithProviders {
    return {
        ngModule: LoggerModule,
        providers: [LoggerService]
    }
  }
}

NgModule of the application:

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    LoggerModule.forRoot()
  ],
  providers: [],
  bootstrap: [AppComponent],
  entryComponents: [AppComponent]
})
export class AppModule {
}
like image 736
JV3 Avatar asked Oct 21 '18 19:10

JV3


1 Answers

Declare your forRoot outside of your module definition:

import { ModuleWithProviders } from ‘@angular/core’;
export const forRoot: ModuleWithProviders = LoggerModule.forRoot();

@NgModule({
   declarations: [
      AppComponent
   ],
   imports: [
        BrowserModule,
       AppRoutingModule,
       forRoot
   ],
   providers: [],
   bootstrap: [AppComponent],
   entryComponents: [AppComponent]
})
export class AppModule {
}
like image 108
pixelbits Avatar answered Nov 03 '22 10:11

pixelbits