0
我正在使用Angular 2与.Net Core和Webpack。我正在尝试将动态模块加载到我的应用程序中。下面是应用程序组件:SystemJsNgModuleLoader“系统未定义”
//our root app component
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import {
\t Component,
\t ViewContainerRef,
\t Compiler,
\t ComponentFactory,
\t ComponentFactoryResolver,
\t ModuleWithComponentFactories,
\t ComponentRef,
\t ReflectiveInjector,
\t SystemJsNgModuleLoader,
\t AfterViewInit,
\t OnInit,
\t NgModuleFactory
} from '@angular/core';
export class ModuleNode { modulePath: string; componentName: string; }
@Component({
selector: 'app',
template: require('./app.component.html'),
styles: [require('./app.component.css')]
})
export class AppComponent implements AfterViewInit {
\t widgetConfig: string;
\t module: ModuleNode;
\t cmpRef: ComponentRef<any>;
\t constructor(private viewref: ViewContainerRef,
\t \t private resolver: ComponentFactoryResolver,
\t \t private loader: SystemJsNgModuleLoader,
\t \t private compiler: Compiler) {
\t \t this.module = new ModuleNode();
\t \t this.module.modulePath = "./dynamic.module";
\t \t this.module.componentName = "TestComponent";
\t }
\t ngAfterViewInit() {
\t \t this.openWebApp(this.module);
\t }
\t ngOnInit() {
\t \t
\t }
\t openWebApp(menu: any) {
\t \t this.loader.load(menu.modulePath) // load the module and its components
\t \t \t .then((modFac) => {
\t \t \t \t this.compiler.compileModuleAndAllComponentsAsync<any>(modFac.moduleType)
\t \t \t \t \t .then((factory: ModuleWithComponentFactories<any>) => {
\t \t \t \t \t \t return factory.componentFactories.find(x => x.componentType.name === menu.componentName);
\t \t \t \t \t })
\t \t \t \t \t .then(cmpFactory => {
\t \t \t \t \t \t // need to instantiate the Module so we can use it as the provider for the new component
\t \t \t \t \t \t let modRef = modFac.create(this.viewref.parentInjector);
\t \t \t \t \t \t this.cmpRef = this.viewref.createComponent(cmpFactory, 0, modRef.injector);
\t \t \t \t \t \t // done, now Module and main Component are known to NG2
\t \t \t \t \t });
\t \t \t });
\t }
\t ngOnDestroy() {
\t \t if (this.cmpRef) {
\t \t \t this.cmpRef.destroy();
\t \t }
\t }
}
在 “this.loader.load(menu.modulePath)” 我得到一个错误 “系统未定义”。 据我所知它可能是SystemJs的一部分,可能会丢失,但在.Net Core和Webpack中,它不应该再被需要。我对吗?
您使用的是哪个版本的webpack? – yurzui
我的版本是: webpack:1.12.14,webpack-externals-plugin:1.0.0,webpack-hot-middleware:2.10.0,webpack-merge:0.14.1, – user3541236
你需要webpack2并检查这个线程https: //github.com/angular/angular-cli/issues/2302 – yurzui