我们可以将System.import或import()函数用于任何Javascript文件,还是仅用于特定于单个模块的js?

问题描述 投票:0回答:1

跟随有关动态组件加载的精彩文章:

https://blog.angularindepth.com/here-is-what-you-need-to-know-about-dynamic-components-in-angular-ac1e96167f9e

我想更多地了解System.import的使用。作者使用以下语法来加载要动态加载的模块的javascript文件。

System.import('../module1.module.js').then((module) => {
          this.compiler.compileModuleAndAllComponentsAsync(module.Module1Module)
              .then((compiled) => {
                  const factory = compiled.componentFactories[0];
                  this.container.createComponent(factory);
              });
      });

我知道system.import用于在运行时动态加载模块。

并且在本例中,我们提供了模块文件ts的名称,但是由于js文件也将具有相同的名称,因此我们可以在import函数的参数中提供该名称。

但是我想知道我们是否可以在参数中提供一个通用的JS文件,该文件不一定包含该模块,但还包含更多的模块和组件。 (这些模块可以来自其他外部项目/库)然后我们可以提供以下模块名称之一并创建组件。

System.import('../main-app1.js').then((module) => {
          this.compiler.compileModuleAndAllComponentsAsync(module.Module1Module)
              .then((compiled) => {
                  const factory = compiled.componentFactories[0];
                  this.container.createComponent(factory);
              });
      });

我已经尝试了上述方法,但是对我不起作用。它在运行时失败。

core.js:9110 ERROR Error: Uncaught (in promise): Error: No NgModule metadata found for 'undefined'.
Error: No NgModule metadata found for 'undefined'.
javascript angular typescript webpack ng-modules
1个回答
0
投票

所以,我将总结我的发现,以便将来对某人有所帮助:

© www.soinside.com 2019 - 2024. All rights reserved.