如何在Angle 2中使用“ SystemJsNgModuleLoader”将动态模块导入到应用程序?

Mehran Mazhar |

Angular网站中,有一个新的api SystemJsNgModuleLoader

剂量有人知道我如何使用此api在应用程序中加载动态模块?

莫汉·雷克斯(Mohan Rex)

我知道您会找到解决方案的,但我为其他人提供参考。该代码已得到很好的注释,并且很好理解。

createComponent(fileName, componentName){
    let injector = ReflectiveInjector.fromResolvedProviders([], this.vcRef.parentInjector);
    // 1. Create module loader
    let loader = new SystemJsNgModuleLoader(this.compiler);
    loader.load(fileName).then((nmf:NgModuleFactory<any>)=>{
        // 2. create NgModuleRef
        let ngmRef = nmf.create(injector);
        // 3. Create component factory
        let cmpFactory = ngmRef.componentFactoryResolver.resolveComponentFactory( componentName );
        // 4. Create the component
        let componentRef = this.span.createComponent(cmpFactory,0,injector,[]);
        // 5. Init the component name field.
        componentRef.instance.name = "Some Name";
        // 6. Refresh the component area.
        componentRef.changeDetectorRef.detectChanges();
        componentRef.onDestroy(()=> {
            componentRef.changeDetectorRef.detach();
        });
    });
}

通过使用以上功能,我们可以从任何来源注入模块。有关更多信息,请参阅

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Android Studio中将后端模块类导入到应用程序模块中

如何将Bower依赖项从Ember-cli插件导入到使用的应用程序中?

您如何正确地将React导入到Node-Webkit应用程序中?

如何将另一个应用程序的res文件夹导入到Android Studio中的应用程序?

如何在Angle 4中使用renderer2动态附加子元素

如何在Wamp中运行Angle 6应用程序?

如何将Apple的UIImage + Effects类别导入到我的Swift应用程序中?

如何在nodejs应用程序中动态导入数据?

将自定义模块导入到Electron应用程序的renderer.js中

将带有 rxjs 的 angular 5 模块导入到 angular 8 应用程序中

如何在Angle 2中使用Codelyzer

如何从 SwiftUI 中的文件应用程序中获取 pdf 文件并将其导入到我的应用程序中使用 iOS

如何使用Koin DI将应用程序上下文从“应用程序”模块注入到“网络”模块

如何将Objective-C文件添加/导入到快速应用程序

将数千条相关记录导入到Rails应用程序中?

如何将Firebase导入到app模块以外的模块中?

如何使用Maven和NetBeans将应用程序打包到JPMS模块中?

如何在Angle 6中设置动态下载链接

如何使用 Javac 将类导入到 Java 程序中?

如何将各个模块中的选择对象导入到 __init__.py 中?

如何将节点模块导入到 svelte 组件中

如何将一个模块从 'base' 环境导入到 anaconda 中的 'tf' 环境?

2sxc 如何在 Visual Query 中使用应用程序设置中的值?

如何在角度2中使用引导程序4?

使用Ng服务将Angle 2+应用程序部署到Cloud Foundry中

如何将静态信息(包括图像)动态导入到ViewController中

如何将RequireJS模块集成到AngularJS应用程序中?

如何在Angle 2应用程序中基于md卡选择显示/隐藏primeng数据表列

如何在 Angular 2 应用程序的 SCSS 文件中使用动态数据?