代码之家  ›  专栏  ›  技术社区  ›  S Panfilov

角度:修饰符中不支持函数表达式

  •  -1
  • S Panfilov  · 技术社区  · 7 年前

    我有一个遗留代码,到目前为止我还没有完全理解,但必须进行维护,哈哈)

    代码是由我在下面描述的组件编写的,它可以与 ng serve ,但与 ng build --prod .

    问题在于 providers 字段:

    @Component({
      selector: "legacy-component",
      templateUrl: './legacy-component.component.html',
      styleUrls: ['./legacy-component.scss'],
      //Take a look at "providers", the problem is there
      providers: [provideWidget(LegacyComponent, {
        id: "legacy",
        options: {},
        layout: { minCols: 2, cols: 4, rows: 2 },
        info: {
          name: "Legacy Compoent",
          image: "assets/img/legacy/legacy.png",
          description: ""
        }
      })]
    })
    export class LegacyComponent {...}
    

    我真的不明白提供商的情况,但当我尝试构建项目时,我有:

    Error during template compile of 'LegacyComponent'
      Function expressions are not supported in decorators in 'provideWidget'
        'provideWidget' contains the error at some/path/other-class.ts
          Consider changing the function expression into an exported function.
    

    但如果我重写 provideWidget 作为函数,我会有不同的错误:

    Error during template compile of 'LegacyComponent'
      Function calls are not supported in decorators but 'provideWidget' was called.
    

    我猜 在提供者中进行函数调用可能不是一个好主意,但我应该启动代码并 我就是无法取代 唯利是图 由于函数的复杂逻辑,使用一些静态数据调用。

    有人知道在这种情况下如何构建生产版本吗?

    1 回复  |  直到 7 年前
        1
  •  -1
  •   Oleksandr Martyniuk    7 年前

    Reference to provider type

    在这里你可以看到提供者可以 TypeProvider | ValueProvider | ClassProvider | ConstructorProvider | ExistingProvider | FactoryProvider .

    所以,如果你想要一个动态的提供者,你需要使用 FactoryProvider :

    { provide: 'SomeToken', useFactory: factoryFunction }
    

    另一种方法是,如果provideWidget是无状态函数,则可以在类定义之前创建一个常量,并将其分配给一个常量值,然后用作 ValueProvider .