代码之家  ›  专栏  ›  技术社区  ›  agascon

角度6-“函数调用在decorators中不受支持”prod build中的错误

  •  1
  • agascon  · 技术社区  · 7 年前

    我目前正在开发一个Angular6应用程序,经过一段时间的开发,当我试图创建一个Prod构建时

    ng build --prod
    

    我遇到了这个错误…

    ERROR in src\app\app.module.ts(26,17): Error during template compile of 'AppModule'
      Function expressions are not supported in decorators in 'APP_ROOT_STATE'
        'APP_ROOT_STATE' references 'APP_ROOT_STATE'
          'APP_ROOT_STATE' contains the error at src\app\app.component.ts(20,16)
            Consider changing the function expression into an exported function.
    

    经过一些研究,我已经清楚了这个错误是关于什么以及如何修复它,但是同时,我在 Angular documentation about AOT 以下内容:

    从版本5开始,编译器在发出.js文件时自动执行重写。

    这意味着什么?通过使用最新版本的Angular和/或Angular CLI包,我得到了上述错误。

    我应该以某种方式启用重写吗? 在不重写元数据中的所有lambda的情况下,拥有AOT还是有希望的?

    错误中引用的代码是…

    export const APP_ROOT_STATE = {
      name: 'app',
      abstract: true,
      views   : {
        header: { component: CoreUiAppHeaderComponent },
        footer: { component: CoreUiAppFooterComponent }
      },
      onEnter: onEnterStateBreadcrumbHelper(new AppBreadcrumbEntryModel('Home', 'default')),
      onExit: onExitStateBreadcrumbHelper(),
      resolve: [
        {
        token: '_appInitialization',
        deps: [AppBootstrapService],
        resolveFn: (bootstrapSvc) => bootstrapSvc.initApplication()
        }
      ]
    };
    

    正是这条线…

    resolveFn: (bootstrapSvc) => bootstrapSvc.initApplication()
    

    如果我将其重写为一个函数并引用其中的函数,那么错误就消失了。像这样的…

    bootstrapSvcinitApplicationFunction = function(bootstrapSvc) {
      bootstrapSvc.initApplication(); 
    }
    ...
    resolveFn: bootstrapSvcinitApplicationFunction
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   kemsky    7 年前

    很可能角度编译器只自动重写那些箭头函数,这些函数用于组件元数据(指令、服务等),换句话说,只支持有限的位置。在您的例子中,函数位于其他一些对象中,所以编译器不知道它是否应该重写。