【问题标题】:Conditionally declare directive in Angular 2 ngModule在 Angular 2 ngModule 中有条件地声明指令
【发布时间】:2017-07-07 05:23:17
【问题描述】:

在 Angular 2 中,只有当全局 javascript 变量设置为 true(调试布尔值)时,我才想在我的 ngModule 中声明一个指令。

这在使用 tsc 编译时有效:

declare let isDebug: boolean;
let dependencyArray : any[] = [];

if ('undefined' !== typeof isDebug && isDebug) {
  dependencyArray.push(DebugDirective);
}

@NgModule({
  declarations: [AppComponent].concat(dependencyArray),
  imports: []
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

然而,ngc 编译器(AOT 编译)似乎不接受模块文件中的函数调用。 ngc 抛出以下错误。

静态解析符号值时遇到错误。不支持函数调用。考虑将函数或 lambda 替换为对导出函数的引用

我找到了各种线程来解释如何将工厂与提供者的导出函数一起使用(例如 https://github.com/angular/angular/issues/11262),但我还没有找到如何为声明数组做同样的事情。

如何解决模块声明的问题?

【问题讨论】:

    标签: angular angular2-aot


    【解决方案1】:

    如何导出一个函数(返回一个 any[])和指向函数引用的声明指令。

    【讨论】:

    • 我已经有一段时间没有尝试过了,我应该检查一下它是如何演变的,但当时这不起作用。
    猜你喜欢
    • 2016-09-26
    • 1970-01-01
    • 2016-08-29
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多