简体   繁体   English

Angular 8 中的插件:未捕获的 ReferenceError:SystemJS

[英]plugin in Angular 8 : Uncaught ReferenceError: SystemJS

I followed this tutorial https://itnext.io/how-to-build-a-plugin-extensible-application-architecture-in-angular5-736890278f3f to install plugins to my angular application, what I want its a component in my angular app which execute and show an external component.我按照本教程https://itnext.io/how-to-build-a-plugin-extensible-application-architecture-in-angular5-736890278f3f将插件安装到我的 angular 应用程序中,我希望它是我的 angular 中的一个组件执行并显示外部组件的应用程序。

The problem is that I got an error:问题是我遇到了一个错误:

Uncaught ReferenceError: SystemJS is not defined
    at Module../src/main.ts (main.ts:13)
    at __webpack_require__ (bootstrap:78)
    at Object.0 (parent.service.ts:13)
    at __webpack_require__ (bootstrap:78)
    at checkDeferredModules (bootstrap:45)
    at Array.webpackJsonpCallback [as push] (bootstrap:32)
    at main.js:1

My main.ts我的main.ts


declare const SystemJS: any;

import * as angularCore from '@angular/core';
import * as angularCommon from '@angular/common';
import * as angularForms from '@angular/forms';

SystemJS.set('@angular/core', SystemJS.newModule(angularCore));
SystemJS.set('@angular/common', SystemJS.newModule(angularCommon));
SystemJS.set('@angular/forms', SystemJS.newModule(angularForms));

a part of angular.json angular.json 的一部分

           "styles": [
              "node_modules/bootstrap/dist/css/bootstrap.css",
              "src/styles.css",
              "node_modules/font-awesome/css/font-awesome.css"

            ],
            "scripts": [
              "node_modules/jquery/dist/jquery.js",
              "node_modules/popper.js/dist/umd/popper.js",
              "node_modules/bootstrap/dist/js/bootstrap.js",
              "node_modules/systemjs/dist/system.js"
            ]

my app.component.ts我的 app.component.ts


import {
  Component, Compiler, Injector, ViewChild,
  ViewContainerRef, AfterViewInit
} from '@angular/core';

declare const SystemJS: any;


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {
  title = 'angularExtensionv3';
  @ViewChild('content', {static: false}) content: ViewContainerRef;

  constructor(private _compiler: Compiler, private _injector: Injector) {
  }

  ngAfterViewInit() {
    this.loadPlugins();
  }

  private async loadPlugins() {
    // import external module bundle
    const module = await SystemJS.import('assets/plugins/plugin-a.bundle.js');

    // compile module
    const moduleFactory = await this._compiler
      .compileModuleAsync<any>(module['PluginAModule']);

    // resolve component factory
    const moduleRef = moduleFactory.create(this._injector);

    // get the custom made provider name 'plugins'
    const componentProvider = moduleRef.injector.get('plugins');

    // from plugins array load the component on position 0
    const componentFactory = moduleRef.componentFactoryResolver
      .resolveComponentFactory<any>(
        componentProvider[0][0].component
      );

    // compile component
    var pluginComponent = this.content.createComponent(componentFactory);

    // sending @Input() values
    // pluginComponent.instance.anyInput = "inputValue";

    // accessing the component template view
    // (pluginComponent.hostView as EmbeddedViewRef<any>).rootNodes[0] as HTMLElement;
  }
}

Someone can help me to fix the error or give me another solution / tutorial to use plugins in angular 8?有人可以帮助我修复错误或给我另一个解决方案/教程来使用 angular 8 中的插件吗?

In your angular.json:在你的 angular.json 中:

"scripts": [
          "./node_modules/jquery/dist/jquery.js",
          "./node_modules/popper.js/dist/umd/popper.js",
          "./node_modules/bootstrap/dist/js/bootstrap.js",
          "./node_modules/systemjs/dist/system.js"
        ]

and the import:和进口:

import * as systemjs from 'systemjs';

Instead of代替

declare const SystemJS: any;

can you try using:你可以尝试使用:

const SystemJs: any = (window as any).System;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM