Angular 7 ERROR ReferenceError:未定义SystemJS

问题描述 投票:3回答:3

我正在使用systemjs创建一个角度7项目来动态加载模块。

当我尝试使用它时,我有这个错误:

 ERROR ReferenceError: SystemJS is not defined

我的package.json包含systemjs:2.1.1

我在angular.json中添加了systemjs路径到section脚本

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

我声明SystemJs在我的服务中使用它:

declare const SystemJS;

而我正试图在这个函数中使用这样的东西:

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;
      });
    });
  }

也许我错过了什么,

你能帮助我吗 ?

angular typescript systemjs angular7
3个回答
4
投票

请务必使用此版本:据我所知,https://github.com/systemjs/systemjs/releases/tag/0.21.5 2.1.1与过去的api不兼容。顺便说一下,在上面的答案的例子中,使用版本“0.21.4 Dev”。


2
投票

从Angular 6开始,你必须提供node_modules的绝对路径。像这样的东西:

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

这是你的参考的Sample StackBlitz

请注意,我在app.component.ts中使用它,我正在记录typeof SystemJS.set


2
投票

我在相应的github问题中回答了这个问题:https://github.com/systemjs/systemjs/issues/1940#issuecomment-490280011

tldr:systemjs @ <2有一个window.SystemJS全局变量,但systemjs @> = 2没有。使用window.SystemSystem而不是SystemJS。你还需要follow the instructions in the systemjs readme关于让你的webpack配置不要搞乱System.import()调用。

© www.soinside.com 2019 - 2024. All rights reserved.