如何手动延迟加载模块?

Sas*_*sxa 45 angular

我已经尝试加载没有使用路由器的模块SystemJsNgModuleLoader,但无法使其工作:

this.loader.load(url).then(console.info);
Run Code Online (Sandbox Code Playgroud)

我正在Cannot find module xxx使用我用于URL的任何字符串(aboslute/relative urls/paths ...尝试了很多选项).我浏览了一下路由器源代码,然后找不到其他任何东西SystemJsNgModuleLoader.我甚至不确定我应该使用这个......


昨天在ng-europe 2016会议上就提出了这个问题--Miško和Matias回答说:

MiškoHevery: 只需要掌握模块,就可以从那里获得组件工厂,您可以在应用程序中的任何位置动态加载组件工厂.这正是路由器内部所做的事情.因此,你也可以做到这一点.

MatiasNiemelä 唯一值得注意的是,在[Ng]模块上有一些被调用的东西entryComponents,它可以识别可以延迟加载的组件 - 这是该组件集的入口.所以当你有懒惰加载的模块时,请把东西放进去entryComponents.

......但是,如果没有关于这个问题的例子和糟糕的文档,那就不是那么紧张的前进了;

任何人都知道如何手动加载模块,而不使用Route.loadChildren?如何掌握模块以及应该进入的内容究竟是什么entryComponents(我阅读常见问题解答,但不能在没有实际加载模块的情况下尝试)?

yur*_*zui 52

任何人都知道如何手动加载模块,而不使用Route.loadChildren?

你可以SystemJsNgModuleLoader用来获取模块的工厂:

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

以下是它的外观:

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;
}
Run Code Online (Sandbox Code Playgroud)

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 {} 
Run Code Online (Sandbox Code Playgroud)
this.loader.load('./src/test.module#TestModule').then((factory: NgModuleFactory<any>) => {
  console.log(factory);
});
Run Code Online (Sandbox Code Playgroud)

Plunker示例

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"
            ]
          }
        }
      }
    }
  }
} 
Run Code Online (Sandbox Code Playgroud)

看到

2)使用来自RouterModule的provideRoutes

app.module.ts

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

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);
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

Github repo angular-cli-lazy


使用webpack和AOT进行延迟加载

使用ngc编译

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

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

Github回购

  • 找到解决方案,但我不知道它是否是正确的解决方案.我必须包含一个延迟加载的路由,以便webpack创建块.这意味着我只需要为此目的提供可访问的路由. (3认同)
  • 你们使用webpack吗?如果是的话,它有效吗? (2认同)
  • @swingmicro你找到了解决方案吗?从外部URL(umd库)加载模块? (2认同)

Rom*_*nLT 10

[Angular 6]

你好,

我在这里分享我的解决方案,因为我没有找到如何在stackoverflow上没有路由器的lazyload.

Yurzui的方式工作,但他使用的路由器模块编译懒模块,而我不希望使用它.

在我们的src/angular.json文件中,我们可以要求@ angular/cli将模块分开编译.

为此,我们lazyModules在"project">"your-app-name">"architect">"build">"options"中添加密钥.

像这样 :

  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects", 
  "projects": {
    "lazy-load-app": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/lazy-custom-element-app",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": [],
            "lazyModules": [
              "src/app/lazy-load/lazy-load.module",
              "src/app/another-lazy-load/another-lazy-load.module"
            ]
Run Code Online (Sandbox Code Playgroud)

然后我们可以调用并加载我们编译的模块.

像这样 :

export class LoaderComponent implements OnInit {

      // tag where we gonna inject the lazyload module and his default compononent "entry"
      @ViewChild('container', { read: ViewContainerRef }) viewRef: ViewContainerRef;

      constructor(
        private loader:     NgModuleFactoryLoader,
        private injector:   Injector,
        private moduleRef:  NgModuleRef<any>,) {
      }

      ngOnInit(): void {
       this.loader.load(this.pathModuleTemplate).then((moduleFactory: NgModuleFactory<any>) => {
          // our module had a static property 'entry' that references the component that  want to load by default with the module
          const entryComponent = (<any>moduleFactory.moduleType).entry;
          const moduleRef = moduleFactory.create(this.injector);
          const compFactory = moduleRef.componentFactoryResolver.resolveComponentFactory(entryComponent);
          this.viewRef.createComponent(compFactory);
        });
      }
}
Run Code Online (Sandbox Code Playgroud)

来源:https://github.com/angular/angular-cli/blob/9107f3cc4e66b25721311b5c9272ec00c2dea46f/packages/angular_devkit/build_angular/src/server/schema.json

希望它可以帮助别人:)