我正在使用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;
});
});
}
也许我错过了什么,
你能帮助我吗 ?
请务必使用此版本:据我所知,https://github.com/systemjs/systemjs/releases/tag/0.21.5 2.1.1与过去的api不兼容。顺便说一下,在上面的答案的例子中,使用版本“0.21.4 Dev”。
从Angular 6开始,你必须提供node_modules
的绝对路径。像这样的东西:
{
...
"projects": {
"demo": {
...,
"architect": {
"build": {
...
"options": {
...
"scripts": [ "node_modules/systemjs/dist/system.js" ]
},
...
},
...
}
}
},
...
}
这是你的参考的Sample StackBlitz。
请注意,我在app.component.ts
中使用它,我正在记录typeof SystemJS.set
我在相应的github问题中回答了这个问题:https://github.com/systemjs/systemjs/issues/1940#issuecomment-490280011
tldr:systemjs @ <2有一个window.SystemJS
全局变量,但systemjs @> = 2没有。使用window.System
或System
而不是SystemJS
。你还需要follow the instructions in the systemjs readme关于让你的webpack配置不要搞乱System.import()
调用。