加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

有角度 – 如何手动懒加载模块?

发布时间:2020-12-17 07:45:47 所属栏目:安全 来源:网络整理
导读:我尝试加载没有路由器的模块使用 SystemJsNgModuleLoader ,但无法使其工作: this.loader.load(url).then(console.info); 我得到无法找到模块xxx的任何字符串我用于URL(aboslute /相对urls /路径…尝试了许多选项).我看过路由器的源代码,找不到任何其他的,然
我尝试加载没有路由器的模块使用 SystemJsNgModuleLoader,但无法使其工作:
this.loader.load(url).then(console.info);

我得到无法找到模块xxx的任何字符串我用于URL(aboslute /相对urls /路径…尝试了许多选项).我看过路由器的源代码,找不到任何其他的,然后这个SystemJsNgModuleLoader.我甚至不确定我应该使用这个…

昨天在ng-europe 2016年会议上,Mi?ko&马蒂亚斯回答说:

Mi?ko Hevery:
One just has to get hold of the module,from there you can get the hold of component factory and you can dynamically load component factory anywhere you want in the application. This is exactly what the router does internally. So it’s quite strait forward for you to do that as well.

Matias Niemel?
The only special thing to note is that on the [Ng]Module there’s something called entryComponents and that identifies components that could be lazy loaded – that’s the entry into that component set. So when you have modules that are lazy loaded,please put the stuff into entryComponents.

…但不是没有例子的海峡前进,

任何人都知道如何手动加载模块,而不使用Route.loadChildren?如何掌握模块,什么是应该进入entryComponents的东西(我读了FAQ,但不能尝试没有实际加载模块)?

Anyone knows how to load modules manually,without using
Route.loadChildren?

它可能看起来像这样:

this.loader.load('./src/test.module#TestModule').then((factory: NgModuleFactory<any>) => {
  console.log(factory);
});

TestModule位于./src/test.module.ts文件(绝对路径)

Plunker Example

这也是更高级的例子

> https://github.com/angular/angular/issues/11738

如果您不加载组件声明如下:

<my-comp></my-comp>

在模板中,如果要使用此组件进行动态加载,则必须通过以下方法之一在entryComponent中引发此组件:

> @NgModule中的entryComponents数组
> @Component中的entryComponents数组
> ANALYZE_FOR_ENTRY_COMPONENTS(角度路由器使用此方法https://github.com/angular/angular/blob/2.1.2/modules/%40angular/router/src/router_module.ts#L198)

编译器创建这样的工厂:

对于每个entryComponent – host.factory.js

对于每个指令 – wrapper.ngfactory.js(对于版本?2)

对于每个组件component.ngfactory.js

也可以看看

> Lazy loading tabs example

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读