AOT-不支持函数调用Module.forRoot [英] AOT - Function calls are not supported Module.forRoot

查看:44
本文介绍了AOT-不支持函数调用Module.forRoot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用带有@ angualar/compiler-cli的AOT来编译我的Angular应用程序,但是在编译过程中却遇到了我无法解决的错误.错误状态:

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.

我有我的主要 AppModule 和另一个名为 PanelModule 的模块.在 PanelModule 中,有一个我从AppModule调用的forRoot方法,如下所示:

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

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

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 ]
        };
    }
}

如您所见,我在 PanelModule 上没有lambda函数.我还看到在forRoot中只能有一个return语句,也只能有一个return语句,但这不是我的情况.我不知道发生了什么事.

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.

推荐答案

我将以此为答案来尝试提供一些帮助并使您前进.我在这个问题上有一定的背景,并且已经根据我的需要解决了这个问题,所以我可以告诉您我在这里看到的内容,也许可以帮上忙.

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.

如果您有任何一种非静态可解析的逻辑,您将在forRoot中得到AOT错误.例如,如果您这样做:

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)
}

它将在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.

但是AOT正在为此而哭泣.因此,AOT编译器无法静态解析forRoot中的某些内容.事实似乎简单地是,forRoot功能尚未真正准备好迎接黄金时段,这是非常幼稚的.例如,您不能直接动态地配置导入,因为ModuleWithProviders不允许您修改导入(实际上可以,但是不能以任何记录的方式).

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).

因此,答案是:我发现可疑的第一件事是您没有向arg提供参数.如果不使用forRoot动态配置提供程序,那么使用forRoot没有什么意义.实际上,我认为forRoot需要一个论点.所以我会检查一下.

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.

如果失败了,我想这是您的进口商品之一,或者是您的提供商之一.这些元素之一就是以某种方式执行了AOT编译器无法静态解决的问题.也许是您的自定义类型?

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.

这篇关于AOT-不支持函数调用Module.forRoot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆