Angular2-从模块动态加载组件

埃切隆

在我的角度应用程序中,我有以下内容:

export class MyComponent {
    subcompPath = "path-to-subcomp#SubcompClassName";
    @ViewChild("placeholder", { read: ViewComponentRef }) placeholderRef: ViewComponentRef;

/* Constructor where Compiler, ComponentFactoryResolver are injected */

    loadSubcomponent() {
        let [path, componentName] = this.subcompPath.split("#");
        (<any>window).System.import(path)
            .then((module: any) => module[componentName])
            .then((type: any) => {
                return this._compiler.compileComponentAsync(type)
            })
            .then((factory: any) => {
                let componentRef = this.placeholderRef.createComponent(factory, 0);
            });
    }
}

我的子组件声明提供程序和东西,指令和管道。

现在,RC6再次打破了一切。组件不能声明指令和管道,但是它们必须在声明该组件的模块中。因此,我必须使用SystemJS加载组件本身而不是模块。好,那我应该用

return this._compiler.compileModuleAndAllComponentsAsync(type)

很好,但是如何获得该特定组件的出厂参考?该工厂就是我所需要的,placeholderRef希望在其createComponent方法中使用它,对吗?

我试图从github上挖掘angular2源代码,但是它相当庞大,我应该使用intellisense从VS Code或其他东西尝试,但是我很懒...我应该从文档中阅读这些东西,这在相当乏味的此特定参数的angular.io,是在没有路由器的情况下延迟加载组件和模块。

感谢您的任何帮助,我认为该解决方案易于应用,但是如果没有官方文档就很难找到。

埃切隆

基于yurzui的答案,我得出以下代码:

export class MyComponent {
    subcompPath = "path-to-subcompMODULE#SubcompClassName";
    @ViewChild("placeholder", { read: ViewComponentRef }) placeholderRef: ViewComponentRef;

    /* Constructor where Compiler, ComponentFactoryResolver are injected */

    loadSubcomponent() {
        let [modulePath, componentName] = this.subcompPath.split("#");
        (<any>window).System.import(modulePath)
            .then((module: any) => module["default"])  // Or pass the module class name too
            .then((type: any) => {
                return this._compiler.compileModuleAndAllComponentsAsync(type)
            })
            .then((moduleWithFactories: ModuleWithComponentFactories<any>) => {
                const factory = moduleWithFactories.componentFactories.find(x => x.componentType.name === componentName); // Crucial: componentType.name, not componentType!!
                let componentRef = this.placeholderRef.createComponent(factory, 0);
            });
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Angular2中动态加载组件

来自分类Dev

在Angular2中动态加载组件

来自分类Dev

加载在angular2 final中动态创建的动态组件

来自分类Dev

Angular2:从服务响应动态加载组件

来自分类Dev

在angular2中使用动态组件加载程序?

来自分类Dev

在Angular2 RC.5中动态加载组件

来自分类Dev

@Output的Angular2动态组件加载器参数

来自分类Dev

在Angular2 RC.5中动态加载组件

来自分类Dev

如何动态加载Angular2根组件

来自分类Dev

动态加载组件后如何让 angular2 更新内容

来自分类Dev

动态加载组件 Angular 2

来自分类Dev

如何在 angular 中使用动态加载的模块组件中的现有组件

来自分类Dev

Angular2 组件或模块 npm 包

来自分类Dev

Angular2 部分动态组件方法

来自分类Dev

Angular2 动态获取组件引用

来自分类Dev

Angular2:无法加载组件

来自分类Dev

Angular2动态加载模板

来自分类Dev

将数据从动态加载的组件传递回父页面Angular2

来自分类Dev

在angular2中动态加载组件时,为什么必须在容器中写入目标?

来自分类Dev

Angular2共享组件与动态不共享组件

来自分类Dev

Angular2测试组件

来自分类Dev

Angular2 父子组件

来自分类Dev

Angular2动态模板

来自分类Dev

找不到模块“ angular2 / angular2”

来自分类Dev

[angular2] [ngModules]如何从其他模块调用组件?

来自分类Dev

Angular2模块无法匹配组件子路线

来自分类Dev

Angular2 RC5模块之间的共享组件

来自分类Dev

在 angular2 组件中需要节点模块

来自分类Dev

Angular,在延迟加载的模块中暴露组件