最新版本的SystemJS中的SystemJS.registerDynamic()等效函数

时间:2019-07-19 09:47:02

标签: javascript angular systemjs

我们有用SystemJS版本 0.21.3 编写的旧代码 我们正在尝试使用最新版本的SystemJS从内存中的JavaScript变量动态加载模块。

有人能指出我最新的registerDynamic替代词,然后import相同吗?

基本上需要在最新的SystemJS中编写此代码。如果可以使用其他也可以的框架完成此任务。

    import * as _ngCore from '@angular/core'
    import * as _ngCommon from '@angular/common'
    import * as _ngHttpClient from '@angular/common/http'
    import * as _ngForms from '@angular/forms';
    import * as _ngbForms from '@ng-bootstrap/ng-bootstrap';
    import * as _dal from 'data-access-layer';
    @Component({
      selector: 'app-custom-mod',
      templateUrl: './custom-mod.component.html',
      styleUrls: ['./custom-mod.component.css']
    })
    export class CustomModComponent implements OnInit {

  ngOnInit() {
    this.LoadComponent()
  }

  LoadComponent() {
    SystemJS.set('@angular/core', SystemJS.newModule(_ngCore));
    SystemJS.set('@angular/common', SystemJS.newModule(_ngCommon));
    SystemJS.set('@angular/forms', SystemJS.newModule(_ngForms));
    SystemJS.set('@angular/common/http', SystemJS.newModule(_ngHttpClient));
    SystemJS.set('@ng-bootstrap/ng-bootstrap', SystemJS.newModule(_ngbForms))


    const normalized = SystemJS.normalizeSync("@customModule-" + this.id);
    if (SystemJS.has(normalized)) {
      SystemJS.delete(normalized);
    }

    SystemJS.registerDynamic("@customModule-" + this.id, ['@angular/core', '@angular/common', '@angular/forms', '@angular/common/http', '@ng-bootstrap/ng-bootstrap', '@dal', '@app'], false, (require, exports, module) => {
      return new Function('require', 'exports', 'module', this.Javascript)(require, exports, module);
    });

    SystemJS.import("@customModule-" + this.id).then((module) => {
          this.compiler.compileModuleAndAllComponentsAsync(module.default)
            .then((compiled) => {
              let moduleRef = compiled.ngModuleFactory.create(this.injector);
              const _injector = moduleRef.injector;

              const widgets = _injector.get('widgets');

              const resolver = moduleRef.componentFactoryResolver;

              const componentFactory = resolver.resolveComponentFactory(widgets[0][0].component);
              this.conponentRef = this.vc.createComponent(componentFactory);
              this.conponentRef.instance.OnLoad();
            });
        })
    }
    }

0 个答案:

没有答案