Angular2 RC5使用正确的模块动态编译组件

Angular2 RC5使用正确的模块动态编译组件,angular,angular2-compiler,Angular,Angular2 Compiler,我试图使用angular2动态插入一个组件,只要所有代码都在默认模块中,它就可以正常工作 但是,如果试图将组件放入它自己的功能模块中,它就会崩溃。 注入的编译器实例()绑定到默认模块而不是功能模块,我无法在编译器调用中重写该模块,因为由于循环依赖关系(功能模块导出组件,组件需要引用功能模块),我无法获取对它的引用 你知道这是个bug还是我做错了什么吗 默认模块: import {NgModule} from '@angular/core'; import {BrowserModule} from

我试图使用angular2动态插入一个组件,只要所有代码都在默认模块中,它就可以正常工作

但是,如果试图将组件放入它自己的功能模块中,它就会崩溃。 注入的编译器实例()绑定到默认模块而不是功能模块,我无法在编译器调用中重写该模块,因为由于循环依赖关系(功能模块导出组件,组件需要引用功能模块),我无法获取对它的引用

你知道这是个bug还是我做错了什么吗

默认模块:

import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';

import {AppComponent} from './app.component';

@NgModule({
    imports: [BrowserModule, FeatureModule],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
import {Component} from "@angular/core";

@Component({
    selector: "my-app",
    template: `
        <h1>Angular2 RC5 Test</h1>
        <feature-component></feature-component>
    `
})
export class AppComponent { }
AppComponent:

import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';

import {AppComponent} from './app.component';

@NgModule({
    imports: [BrowserModule, FeatureModule],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
import {Component} from "@angular/core";

@Component({
    selector: "my-app",
    template: `
        <h1>Angular2 RC5 Test</h1>
        <feature-component></feature-component>
    `
})
export class AppComponent { }
功能组件

import { NgModule }     from "@angular/core";
import { CommonModule } from "@angular/common";
import { FormsModule }  from "@angular/forms";

import { FeatureComponent }  from "./feature.component";
import { FeatureService }    from "./feature.service";


@NgModule({
    imports: [CommonModule, FormsModule],
    declarations: [FeatureComponent],
    exports: [FeatureComponent],
    providers: [FeatureService]
})
export class FeatureModule { }
import {Component, Compiler, ComponentFactory} from "@angular/core";

import {FeatureService} from "./feature.service";

// import {FeatureModule} from "./feature.module"; // produces error if referenced due to circular dependency


@Component({
    selector: "feature-component",
    template: "<div>Feature Component</div>"
})
export class FeatureComponent {

    constructor(private _service: FeatureService, private _compiler: Compiler) {
        // compiler currently belongs to AppModule (should be FeatureModule)
        console.log((<any>this._compiler)._ngModule); // outputs function AppModule() { }
    }

    public createComponent(component: any): void {
        this._compiler.compileComponentAsync(component /* , could override module here, but can't get reference */)
            .then((factory: ComponentFactory<any>) => {
                 // ...
            });
    }
}
从“@angular/core”导入{Component,Compiler,ComponentFactory};
从“/feature.service”导入{FeatureService};
//从“/feature.module”/”导入{FeatureModule}如果由于循环依赖关系而引用,则生成错误
@组成部分({
选择器:“功能组件”,
模板:“要素组件”
})
导出类功能组件{
构造函数(专用服务:FeatureService,专用编译器:编译器){
//编译器当前属于AppModule(应为FeatureModule)
log((this._编译器)。_ngModule);//输出函数AppModule(){}
}
public createComponent(组件:任意):void{
此._compiler.compileComponentAsync(component/*,可以在此处重写模块,但无法获取引用*/)
.然后((工厂:组件工厂)=>{
// ...
});
}
}

我遇到了完全相同的问题,我能找到的最好的答案是Angular 2的一个bug,以及编译模块在不使用加载的模块上下文的情况下是如何工作的。Compiler.compileComponentAsync()已在RC6中删除,请参阅以获取替代方法。