首页 > 解决方案 > 模块“DynamicHtmlModule”导入的意外值“IonicModule”。请添加@NgModule 注释

问题描述

我试图在抛出后追踪这段代码:

Unexpected value 'IonicModule' imported by the module 'DynamicHtmlModule'. Please add a @NgModule annotation.

该代码根据动态使用的模块集通过模板创建动态 HTML 组件。该代码的工作原理如下:

createAndCompileComponent(alertCtrl: AlertController, template: string, componentClass: any, extraImports: any[] = []): Promise<ComponentFactory<any>> {
    // Create the component using the template and the class.
    const component = Component({
        template: template
    })
    (componentClass);

    const imports = this.IMPORTS.concat(extraImports);

    // Now create the module containing the component.
    @NgModule({imports: imports, declarations: [component]})
    class DynamicHtmlModule { }

    try {
        // Compile the module and the component.
        return this.compiler.compileModuleAndAllComponentsAsync(DynamicHtmlModule).then((factories) => {
            // Do some stuff
        });
    } catch (ex) {
        let message = 'Template has some errors and cannot be displayed.';
        return Promise.reject({message: message, debuginfo: ex});
    }
}

依赖模块的“imports”数组由两部分组成:

  1. 静态部分:

    protected IMPORTS = [ IonicModule, TranslateModule.forChild(), // 其他一些本地创建的模块];

  2. 动态部分,这是extraImports奇怪的部分,当我在控制台中记录这个数组时,它是空的!!,所以我想也许这个特定的 HtmlComponent 不需要额外的,所以我尝试用一​​个硬替换它编码空数组,令人惊讶的是它甚至在达到原始错误之前就中断了。

标签: angularionic-framework

解决方案


推荐阅读