【问题标题】:Why run-time populated array does not work on NgModule declarations?为什么运行时填充数组不适用于 NgModule 声明?
【发布时间】:2020-03-10 12:53:47
【问题描述】:

虽然固定数组有效?

我想使用这个从路由中提取组件的工具:

function* getRoutesComponents(routes: Routes) {
    for (const route of routes) {
        if (route.component) {
            yield route.component;

            if (route.children) {
                yield* getRoutesComponents(route.children);
            }
        }
    }
}



@NgModule({
    imports: [SharedModule, RouterModule.forChild(ROUTES)],
    declarations: getRoutesComponents(ROUTES)
})
export class ProductModule {}    

所以我可以避免在声明属性中丢失组件声明。声明中缺少组件会导致错误:NG8002: Can't bind to 'ngModel' since it isn't a known property of 'input'

【问题讨论】:

  • 我已经尝试在stackblitz.com/edit/… 的示例应用程序中复制您的场景,它只能正常工作.. childcomponentappcomponent 被推入 a 并已加载..
  • @ManirajMurugan 对您的示例作品感兴趣。我正在使用这个例子:github.com/DeborahK/Angular-Routing。也许该项目上有一些角度或打字稿设置使角度或打字稿静态读取 NgModule

标签: angular


【解决方案1】:

要为声明启用runtime population of components,我们需要禁用Angular 的AOT。这可以通过将ng serveng build--aot 设置为false 来完成。

但是,禁用 AOT 会带来两大不便。首先,我们不能再使用常量作为对象的键。其次,当 AOT 被禁用时,尽管 fullTemplateTypeCheck 设置为 true,Angular 仍然会构建项目,即使绑定到 ngModel 的名称有拼写错误或拼写错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-23
    • 2014-01-23
    • 2021-11-08
    相关资源
    最近更新 更多