2017-03-31 62 views
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中,它不應該再被需要。我對嗎?

+0

您使用的是哪個版本的webpack? – yurzui

+0

我的版本是: webpack:1.12.14,webpack-externals-plugin:1.0.0,webpack-hot-middleware:2.10.0,webpack-merge:0.14.1, – user3541236

+1

你需要webpack2並檢查這個線程https: //github.com/angular/angular-cli/issues/2302 – yurzui

回答

0

感謝yurzui, 我將webpack更改爲版本2,錯誤消失。

但現在我有一個問題找到模塊。我得到「vendor.js:59947 EXCEPTION:錯誤./AppComponent類AppComponent_Host - 內聯模板:0:0引起:無法找到模塊'./dynamic.module'。」

我也試過」 ./dynamic.module.ts','./ClientApp/app/components/app/dynamic.module.ts' ,需要加載之前( 「./ dynamic.module.ts」)從文件。