我曾在一个Angular 2
项目中使用过es6-module-loader
,并且它非常适合在web浏览器中实时加载TypeScript
模块。现在,我正在将该项目升级到Angular 6
,但是这里加载模块的imports
未满足依赖项。例如:
declare var SystemLoader:any;
export class DemoClass {
constructor() {
var source = "export class Foo { " +
"constructor() { console.log('Created the ES6 class foo!'); } " +
"execMethod() { console.log('Executed method!') }" +
"}";
SystemLoader.module(source, {name: _name}).then(function (module: any) {
module.Foo.prototype.execMethod();
}
}
}
这段原始代码适用于Angular 6
。它将加载模块Foo
并在Console
中打印这些行。但是,如果我增加一些像这样的import
使模块变得更加复杂:
declare var SystemLoader:any;
export class DemoClass {
constructor() {
var source = "import {Component} from \"@angular/core\"; " +
"export class Foo { " +
"constructor() { console.log('Created the ES6 class foo!'); } " +
"execMethod() { console.log('Executed method!') }" +
"}";
SystemLoader.module(source, {name: _name}).then(function (module: any) {
module.Foo.prototype.execMethod();
}
}
}
然后它会报错,显示error 404 loading @angular/core
。所以在Angular 2中没有问题,因为所有项目所需的node_modules
都由Angular直接加载,但是在Angular 6中,似乎所有这些依赖关系都被Webpack捆绑在一个巨大的JavaScript
文件中。那么,我该如何解决Webpack的这种简化方式,以便动态模块可以加载?编辑:
或者至少提供一个示例,以相同的过程从
es6-module-loader
(已弃用)迁移到es-module-loader
(加载源代码、编译[转译]并在客户端机器上呈现)。
es6-module-loader
已经废弃,因此可能会出现支持方面的问题。请尝试升级到 es-module-loader。 - dRoyson