装饰器不支持Angular 6 Prod函数调用,但调用了“..Module” [英] Angular 6 Prod Function calls are not supported in decorators but '..Module' was called

查看:209
本文介绍了装饰器不支持Angular 6 Prod函数调用,但调用了“..Module”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用angular2-json-schema-form&时出现此错误在prod中构建

I have this error when trying to use angular2-json-schema-form & build in prod


在'DemoModule'的模板编译过程中出现错误错误
装饰器不支持函数调用,但是'JsonSchemaFormModule'被称为。
我发现错误来自:

ERROR in Error during template compile of 'DemoModule' Function calls are not supported in decorators but 'JsonSchemaFormModule' was called. I found out that the the error comes from:



@NgModule({
  declarations: [ AceEditorDirective, DemoComponent, DemoRootComponent ],
  imports: [
    BrowserModule, BrowserAnimationsModule, FlexLayoutModule, FormsModule,
    HttpClientModule, MatButtonModule, MatCardModule, MatCheckboxModule,
    MatIconModule, MatMenuModule, MatSelectModule, MatToolbarModule,
    RouterModule.forRoot(routes),

    NoFrameworkModule, MaterialDesignFrameworkModule,
    Bootstrap3FrameworkModule, Bootstrap4FrameworkModule,

    JsonSchemaFormModule.forRoot(   // the source of the Error
      NoFrameworkModule,
      MaterialDesignFrameworkModule,
      Bootstrap3FrameworkModule,
      Bootstrap4FrameworkModule
    )
  ],
  bootstrap: [ DemoRootComponent ]
}) 

在图书馆的源代码中,他们使用以下内容:

In the source code of the library they use something like this:

export class JsonSchemaFormModule {
  static forRoot(...frameworks): ModuleWithProviders {
    const loadFrameworks = frameworks.length ?
      frameworks.map(framework => framework.forRoot().providers[0]) :
      [{ provide: Framework, useClass: NoFramework, multi: true }];
    return {
      ngModule: JsonSchemaFormModule,
      providers: [
        JsonSchemaFormService, FrameworkLibraryService, WidgetLibraryService,
        ...loadFrameworks
      ]
    };
  }
}

当我删除变量 loadFramworks 我没有错误,但我似乎无法在providers属性中注入框架

When I remove the variable loadFramworks I don't have the error but I can't seem to in inject the frameworks in the providers attribute

推荐答案

正如另一个答案所说,这是AOT的一个问题。在装饰器中调用的函数(如 @NgModule )必须适合编译器进行静态分析。确切的必要条件是棘手的,因为它们取决于使用的库和工具的许多因素和版本。

It's a problem with AOT as the other answer says. The functions that are called in a decorator like @NgModule must be amenable to static analysis by the compiler. The exact requisites for that are tricky as they depend on a number of factors and versions of the libraries and tools in use.

当违规函数在您拥有的库中时,还有行动的余地。在我的例子中,例如,我决定从函数中删除所有局部变量,将其减少为复杂表达式的单个return语句,除了 RouterModule.forRoot 之外没有函数调用这是众所周知的。例如,参见角度Flex布局中的提交make withConfig AOT compatible项目。使用Angular的GitHub问题的一些评论者也可以使用 tsconfig.lib.json 中的角度编译器选项进行操作。

When the offending function is in a library you own, there's room for action. In my case, for example, I resolved to removing all local variables from the function, reducing it to a single return statement of a complex expression with no function calls except RouterModule.forRoot which is known to work. See for example the commit "make withConfig AOT compatible" in the Angular Flex Layout project. Playing with the angular compiler options in tsconfig.lib.json is also documented to work by some commenters on Angular's GitHub issues.

但是,在您的情况下,您似乎无法控制有问题的模块和功能。你一定要把它报告给模块的开发人员。

In your case, though, it looks like you're not in control of the offending module and function. You should definitely report it to the module's developers.

这篇关于装饰器不支持Angular 6 Prod函数调用,但调用了“..Module”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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