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

2

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 {
}
Manatarms answered 21/10, 2018 at 19:1 Comment(4)
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: angular.io/guide/aot-compiler#phase-1-analysisBurgener
Just edited my question including the NgModule definitions.Manatarms
Please show your ngModule with "LibModule" imported (that is where the error is being reported)Burgener
Sorry that was a typing error...Manatarms
B
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 {
}
Burgener answered 21/10, 2018 at 23:27 Comment(7)
Make sure you import ModuleWithProvidersBurgener
Yes, it is imported.Manatarms
Could you show your module that has "'LibModule" imported?Burgener
Not sure, is this the actual code that shows the error, or is this a simplified version of it? Does LoggerModule have a forRoot with arguments? I see nothing wrong with your codeBurgener
Yeah, that's all. I don't think that the code of any module is the problem. I create the bundle of the library with ng-packagr. Maybe I have to set some angularCompilerOptions or something else?Manatarms
Do you by chance have an incorrect import? Ie instead of importing the package, autocomplete pointed you to a src directory - that would be my guessBurgener
Excelent!!! After many trieds, finally that work! ThanksAilssa
P
0

put

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

into your tsconfig.json

Penza answered 25/3, 2021 at 22:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.