gpt4 book ai didi

angular - 如何手动延迟加载模块?

转载 作者:太空狗 更新时间:2023-10-29 16:47:23 27 4
gpt4 key购买 nike

我试过使用 SystemJsNgModuleLoader 在没有路由器的情况下加载模块, 但无法让它工作:

this.loader.load(url).then(console.info);

我得到 Cannot find module xxx 用于 URL 的任何字符串(aboslute/relative urls/paths...尝试了很多选项)。我查看了 Router 源代码,除了这个 SystemJsNgModuleLoader 之外找不到任何东西。我什至不确定我应该使用这个...


这个问题是昨天在 ng-europe 2016 session 上提出的 - Miško 和 Matias 回答:

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?

您可以使用 SystemJsNgModuleLoader 获取模块的工厂:

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

For Angular 8 see Lazy load module in angular 8

它看起来像这样:

lazy.module.ts

@Component({
selector: 'test',
template: `I'm lazy module`,
})
export class Test {}

@NgModule({
imports: [CommonModule],
declarations: [Test],
entryComponents: [Test]
})
export class LazyModule {
static entry = Test;
}

app.ts

import {
Component, NgModule, ViewContainerRef,
SystemJsNgModuleLoader, NgModuleFactory,
Injector} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'

@Component({
selector: 'my-app',
template: `<h2>Test lazy loading module</h2>`,
})
export class AppComponent {
constructor(
private loader: SystemJsNgModuleLoader,
private inj: Injector,
private vcRef: ViewContainerRef) {}

ngOnInit() {
this.loader.load('./src/lazy.module#LazyModule')
.then((moduleFactory: NgModuleFactory<any>) => {
const moduleRef = moduleFactory.create(this.inj);
const entryComponent = (<any>moduleFactory.moduleType).entry;
const compFactory =
moduleRef.componentFactoryResolver.resolveComponentFactory(entryComponent);
this.vcRef.createComponent(compFactory);
});
}
}

@NgModule({
imports: [ BrowserModule ],
declarations: [ AppComponent ],
providers: [SystemJsNgModuleLoader],
bootstrap: [ AppComponent ]
})
export class AppModule {}
this.loader.load('./src/test.module#TestModule').then((factory: NgModuleFactory<any>) => {
console.log(factory);
});

Plunker Example

AOT预编译模块有两种选择:

1) Angular CLI lazyModules 选项(自 Angular 6 起)

使用 angular/cli 内置功能:

{
"projects": {
"app": {
"architect": {
"build": {
"options": {
"lazyModules": [ <====== add here all your lazy modules
"src/path-to.module"
]
}
}
}
}
}
}

2) 使用 RouterModule 中的 provideRoutes

app.module.ts

providers: [
SystemJsNgModuleLoader,
provideRoutes([
{ loadChildren: 'app/lazy/lazy.module#LazyModule' }
])
],

app.component.ts

export class AppComponent implements  OnInit {
title = 'Angular cli Example SystemJsNgModuleLoader.load';

@ViewChild('container', { read: ViewContainerRef }) container: ViewContainerRef;

constructor(private loader: SystemJsNgModuleLoader, private inj: Injector) {}

ngOnInit() {
this.loader.load('app/lazy/lazy.module#LazyModule').then((moduleFactory: NgModuleFactory<any>) => {
const entryComponent = (<any>moduleFactory.moduleType).entry;
const moduleRef = moduleFactory.create(this.inj);

const compFactory = moduleRef.componentFactoryResolver.resolveComponentFactory(entryComponent);
this.container.createComponent(compFactory);
});
}
}

Github repo angular-cli-lazy


使用 webpack 和 AOT 进行延迟加载

使用ngc编译

使用以下工厂初始化编译器

export function createJitCompiler () {
return new JitCompilerFactory([{useDebug: false, useJit: true}]).createCompiler();
}

Github repo

关于angular - 如何手动延迟加载模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40293240/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com