简体   繁体   English

Angular 7 ERROR ReferenceError:未定义SystemJS

[英]Angular 7 ERROR ReferenceError: SystemJS is not defined

I'm creating an angular 7 project with systemjs to load dynamically a module. 我正在使用systemjs创建一个角度7项目来动态加载模块。

When I try to use it, I have this error: 当我尝试使用它时,我有这个错误:

 ERROR ReferenceError: SystemJS is not defined

My package.json contain systemjs: 2.1.1 我的package.json包含systemjs:2.1.1

I added systemjs path to section scripts in my angular.json 我在angular.json中添加了systemjs路径到section脚本

"./node_modules/systemjs/dist/system.js"

I declared SystemJs to use it in my service: 我声明SystemJs在我的服务中使用它:

declare const SystemJS;

and I'm trying to using like this, in this function: 而我正试图在这个函数中使用这样的东西:

loadModuleSystemJS(moduleInfo: ModuleData): Promise<any> {
    const url = this.source + moduleInfo.location;
    SystemJS.set('@angular/core', SystemJS.newModule(AngularCore));
    SystemJS.set('@angular/common', SystemJS.newModule(AngularCommon));
    SystemJS.set('@angular/router', SystemJS.newModule(AngularRouter));
    SystemJS.set('@angular/platform-browser/animations', SystemJS.newModule(BrowserAnimations));

    // now, import the new module
    return SystemJS.import(`${url}`).then((module) => {
      return this.compiler.compileModuleAndAllComponentsAsync(module[`${moduleInfo.moduleName}`]).then(compiled => {
        return module;
      });
    });
  }

Maybe I've missed something, 也许我错过了什么,

Can you help me ? 你能帮助我吗 ?

Be sure to use this release: https://github.com/systemjs/systemjs/releases/tag/0.21.5 2.1.1 is not compatible with past api, as far as I can tell. 请务必使用此版本: https//github.com/systemjs/systemjs/releases/tag/0.21.5 2.1.1与过去的api不兼容,据我所知。 By the way, in the example from the answer above, however, the version "0.21.4 Dev" is used. 顺便说一下,在上面的答案的例子中,使用版本“0.21.4 Dev”。

Since Angular 6, you have to provide the absolute path from node_modules . 从Angular 6开始,您必须提供node_modules的绝对路径。 Something like this: 像这样的东西:

{
  ...
  "projects": {
    "demo": {
      ...,
      "architect": {
        "build": {
          ...
          "options": {
            ...
            "scripts": [ "node_modules/systemjs/dist/system.js" ]
          },
          ...
        },
        ...
      }
    }
  },
  ...
}

Here's a Sample StackBlitz for your ref. 这是你的ref的Sample StackBlitz

Note that I've used it in app.component.ts where I'm logging the typeof SystemJS.set 请注意,我在app.component.ts中使用它,我正在记录typeof SystemJS.set

I answered this in the corresponding github issue: https://github.com/systemjs/systemjs/issues/1940#issuecomment-490280011 我在相应的github问题中回答了这个问题: https//github.com/systemjs/systemjs/issues/1940#issuecomment-490280011

tldr: systemjs@<2 had a window.SystemJS global variable, but systemjs@>=2 does not. tldr:systemjs @ <2具有window.SystemJS全局变量,但systemjs @> = 2没有。 Use window.System or System instead of SystemJS . 使用window.SystemSystem ,而不是SystemJS You'll also need to follow the instructions in the systemjs readme about getting your webpack config to not mess with the System.import() calls. 您还需要按照systemjs自述文件中的说明进行操作,以使您的webpack配置不会弄乱System.import()调用。

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

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