【问题标题】:Load NgModule entryComponents dynamically using service使用服务动态加载 NgModule entryComponents
【发布时间】:2017-05-03 04:53:02
【问题描述】:

您好,我是 angular2 和 typescript 的新手。 我正在动态加载组件,我想知道是否有一种方法可以使用模块的“声明”和“入口组件”中的服务来声明我的组件。

使用打字稿,我可以通过执行以下操作来实现它:

import { ComponentLoaderService } from './../../services/component-loader.service';

let componentsToLoad = ComponentLoaderService.getComponents();

@NgModule({
  declarations: [ componentsToLoad ],
  entryComponents: [ componentsToLoad ],
})
export class testModule {}

这确实有效,但前提是我已经编译并且服务器首先运行。

如果我尝试重新编译并运行它,我会得到这个持续错误:

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

我的另一个想法是,有没有办法将组件的加载放在“export class testModule {}”部分来填充数组,然后将其传递给 NgModule?

从我目前的测试来看,它不起作用,但我还是新手,所以我可能会遗漏一些东西。

希望有人可以提供帮助。 谢谢!

这是产生编译错误的代码:

我刚刚做了一个新的测试应用程序。

然后我在 test-app 文件夹中安装了 npm。

我创建了 /src/app/services/components-loader.service.ts。

import { Injectable } from '@angular/core';
import { ViewContainerRef, ViewChild, ComponentFactoryResolver } from '@angular/core';

@Injectable()
export class ComponentLoaderService {
    constructor(private componentFactoryResolver: ComponentFactoryResolver){}

    static getComponents(components: any[]): any[] {
        var tmp = Array();

        for (var i = 0; i < components.length; ++i){
            if (components[i].key == 0){
                tmp.push(components[i].component);
            }
        }

        return tmp;
    }

    load(container: ViewContainerRef, components: any[]): void {
        // clear 
        container.clear();

        for (var i = 0; i < components.length; ++i){
            if (components[i].key == 0 || components[i].key == 'site'){
                const childComponent = this.componentFactoryResolver.resolveComponentFactory( components[i].component );

                // at this point we want the "child" component to be rendered into the app.component:
                container.createComponent(childComponent);          
            }            
        }
    }
}

我创建了一个文件 /src/app/components.ts。

import { TestComponent } from './test.component';

export const mainComponents = [
    { key: 0, component: TestComponent },        
];

我创建了一个文件 /src/app/test.component.ts。

import { Component } from '@angular/core';

@Component({
  template: `test`,
})
export class TestComponent {}

我将 /src/app/app.module.ts 修改为如下所示。

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';
import { mainComponents } from './components';
import { ComponentLoaderService } from './services/components-loader.service';

let componentsToLoad = ComponentLoaderService.getComponents(mainComponents);

@NgModule({
  declarations: [
    AppComponent,
    componentsToLoad
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule
  ],
  providers: [],
  bootstrap: [AppComponent],
  entryComponents: [ componentsToLoad ],
})
export class AppModule { }

当我使用 ng serve 编译时,我得到这个错误:

10% 构建模块 2/2 模块 0 activeError: 遇到错误 静态解析符号值。不支持函数调用。 考虑将函数或 lambda 替换为对 导出函数,解析符号 AppModule 中 D:/angularjs/test-app/src/app/app.module.ts,解析符号 D:/angularjs/test-app/src/app/app.module.ts中的AppModule

【问题讨论】:

标签: angular typescript dynamic components


【解决方案1】:
@NgModule({
    declarations: [],
    exports: []
})
export class testModule {
    static withComponents(components: any[]) {
        return {
            ngModule: testModule,
            providers: [
                {provide: ANALYZE_FOR_ENTRY_COMPONENTS, useValue: components, multi: true}
            ]
        }
    }
}

其他模块:

import { ComponentLoaderService } from './../../services/component-loader.service';

let componentsToLoad = ComponentLoaderService.getComponents();

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        testModule.withComponents([
            ...componentsToLoad
        ])
    ],
    declarations: [
        AppComponent,
        ...componentsToLoad
    ],
    bootstrap: [AppComponent]
})
export class AppModule {
}

通过在此处使用 ANALYZE_FOR_ENTRY_COMPONENTS,您可以将多个组件动态添加到 NgModule.entryComponents 条目中。

【讨论】:

  • 感谢您的回答!但是我如何使它也适用于声明呢?我不能使用我在@NgModule 之前编写的代码部分,因为它一直给我编译错误。
  • 我已经在我的原始帖子中包含了上面的示例代码。 @FunStuff
猜你喜欢
  • 2017-02-06
  • 2017-11-17
  • 1970-01-01
  • 2017-03-06
  • 2020-10-18
  • 1970-01-01
  • 2014-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多