7

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 {
}
JV3
  • 872
  • 2
  • 9
  • 21
  • Please show your NgModule definition. This error usually occurs if you're using a language feature that is currently not supported by the AOT compiler. The AOT compiler only understands a subset of Javascript: https://angular.io/guide/aot-compiler#phase-1-analysis – Michael Kang Oct 21 '18 at 19:27
  • Just edited my question including the NgModule definitions. – JV3 Oct 21 '18 at 20:20
  • Please show your ngModule with "LibModule" imported (that is where the error is being reported) – Michael Kang Oct 22 '18 at 15:07
  • Sorry that was a typing error... – JV3 Oct 22 '18 at 15:42

2 Answers2

1

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 {
}
Michael Kang
  • 52,003
  • 16
  • 103
  • 135
0

put

"angularCompilerOptions": { "fullTemplateTypeCheck": true, "strictInjectionParameters": true }

into your tsconfig.json