AOT - Function calls are not supported Module.forRoot

萝らか妹 提交于 2021-01-29 04:38:39

问题


I've been trying to compile my Angular app using AOT with @angualar/compiler-cli but I've been ending up with an error during compiling which I am not able to solve. The error states:

Error encountered resolving symbol values statically. 
Calling function 'PanelModule', function calls are not supported.
Consider replacing the function or lambda with a reference to an exported function.

I have my main AppModule and another module called, PanelModule. In PanelModule I have a forRoot method that I am calling from the AppModule like this:

app.module.ts / AppModule

@NgModule( {
    imports: [
        BrowserModule,
        routing,
        PanelModule.forRoot(), <----------- If not present it compiles OK
    ],
    declarations: [
        AppComponent,
        LoginView,
        WorkbenchView,
        ErrorView,
        NotFoundErrorView,
        DashboardView,
        BackgroundVideoComponent
    ],
    providers: [
        {
            provide: APP_BASE_HREF,
            useValue: URL_BASE
        },
        CARBON_PROVIDERS,
        CARBON_SERVICES_PROVIDERS,
        appRoutingProviders,
        Title,
    ],
    bootstrap: [ AppComponent ],
} )
export class AppModule {}

panel.module.ts / PanelModule

@NgModule( {
    imports: [
        CommonModule,
        RouterModule,
        SemanticModule,
        DirectivesModule,
        FormsModule
    ],
    declarations: [
        HeaderComponent,
        ...
        PaginatorComponent,
    ],
    exports: [
        HeaderComponent,
        ...
        PaginatorComponent,
    ],
    providers: []
} )

export class PanelModule {

    static forRoot():ModuleWithProviders {
        return {
            ngModule: PanelModule,
            providers: [ HeaderService, SidebarService, RouterService, MyAppsSidebarService, ErrorsAreaService ]
        };
    }
}

As you can see, I have no lambda functions on PanelModule. I've also seen that you can have only one return statement and only a return statement in forRoot but that's not my case. I have no idea of what's going on.


回答1:


I will hazard this as an answer to try and provide some help and get you moving forward. I've got a bit of background on this issue and have solved it for my needs, so I can tell you what I see here and maybe it can help.

You will get the AOT error in your forRoot if you have ANY kind of non-statically-resolvable logic. For example, if you did this:

( passing this obj as args: "{ production: true }" )

forRoot ( args ) {
 if ( args.production )...
 (or a ternary, e.g. args.production ? this : that)
}

It will crash in AOT.

Even exporting functions and using factories etc, which is the oft-recommended "fix", did not turn out to be consistent and reliable. I tried every single recommended configuration of compiler properties etc. that I could find, all to no avail (as many others have encountered).

There's a surprisingly straightforward way around this, but clearly that's not your issue. You have no conditionals, function calls, or anything.

But AOT is crying about it. So somehow, something that you have in forRoot can not be statically resolved by the AOT compiler. The fact simply seems to be, the forRoot capability is not really ready for prime time, it's pretty naive. For instance, you can't dynamically configure imports straightforwardly, because ModuleWithProviders doesn't let you modify imports (you can actually, but not in any documented way).

So to the answer: the first thing I find suspicious, is the fact that you are feeding no args to forRoot. There is not much point in using forRoot if you are not using it to dynamically configure providers. In fact I think forRoot requires an argument. So I'd check that out.

Failing that, my guess would be, it's one of your imports, or one of your providers. One of those elements is somehow doing something that the AOT compiler can't resolve statically. Perhaps your custom types?

Me, I'd remove them all and try the compile. If it works, you know where the problem lies. If it does not, then the problem is higher up the tree.

With the entire (compilable) codebase, I could probably help you more. But for now, this is how I pursued this problem when I crashed into it. I managed to narrow down the problematic element, which got me moving towards a solution.



来源:https://stackoverflow.com/questions/42427524/aot-function-calls-are-not-supported-module-forroot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!