在angular2 final中动态加载组件?
我最近将angular2应用升级到了最终版本 我试图从网上提供的大量参考资料中实现,但没有成功 在angular2 RC5中,我使用编译器动态加载组件,如下所示:在angular2 final中动态加载组件?,angular,typescript,Angular,Typescript,我最近将angular2应用升级到了最终版本 我试图从网上提供的大量参考资料中实现,但没有成功 在angular2 RC5中,我使用编译器动态加载组件,如下所示: @Input('component-name') componentName: string; @Input('component-model') componentModel: any; @ViewChild('target', { read: ViewContainerRef }) target: ViewContainerRef
@Input('component-name') componentName: string;
@Input('component-model') componentModel: any;
@ViewChild('target', { read: ViewContainerRef }) target: ViewContainerRef;
private cmpRef: ComponentRef<any>;
public toSet: any;
keysRegister: string[] = [
'BASIC-CAROUSEL',
'BS-JUMBOTRON'
]
componentNames: string[]=[
"BasicCarouselComponent",
"BsJumbotronComponent"
]
constructor(private _compiler: Compiler, private _viewContainerRef:ViewContainerRef) { }
ngOnInit() {
this.toSet={
'componentModel':this.componentModel,
'componentInfo':this.componentInfo
};
}
ngAfterViewInit(): void {
let componentIndex = this.keysRegister.indexOf(this.componentName);
console.log(this.componentNames[componentIndex]);
if (this.cmpRef) {
this.cmpRef.destroy();
}
this._compiler.compileComponentAsync(StandardLib[this.componentNames[componentIndex]]).then(comp => {
let ref = this.target.createComponent(comp);
if (this.toSet) {
const keys = Object.keys(this.toSet);
keys.forEach(a => ref.instance[a] = this.toSet[a])
}
});
}
ngOnDestroy() {
if (this.cmpRef) {
this.cmpRef.destroy();
this.cmpRef = null;
}
}
@Input('component-name')componentName:string;
@输入(“组件模型”)组件模型:任意;
@ViewChild('target',{read:ViewContainerRef})target:ViewContainerRef;
私有cmpRef:ComponentRef;
公共toSet:任何;
密钥寄存器:字符串[]=[
“基本旋转木马”,
“BS-JUMBOTRON”
]
组件名称:字符串[]=[
“BasicCarouselComponent”,
“BsJumbotronComponent”
]
构造函数(private _编译器:编译器,private _viewContainerRef:viewContainerRef){}
恩戈尼尼特(){
这是我的={
“componentModel”:this.componentModel,
“componentInfo”:this.componentInfo
};
}
ngAfterViewInit():void{
让componentIndex=this.keysRegister.indexOf(this.componentName);
log(this.componentNames[componentIndex]);
if(this.cmpRef){
this.cmpRef.destroy();
}
this.\u compiler.compileComponentAsync(标准库[this.componentNames[componentIndex]])。然后(comp=>{
设ref=this.target.createComponent(comp);
如果(此.toSet){
const keys=Object.keys(this.toSet);
keys.forEach(a=>ref.instance[a]=this.toSet[a])
}
});
}
恩贡德斯特罗(){
if(this.cmpRef){
this.cmpRef.destroy();
this.cmpRef=null;
}
}
其中,我使用toSet
设置组件的实例变量。
但随着angular2的发布,这种方法已被弃用。
我不知道如何在安格拉尔2决赛中做到这一点
有什么意见吗
提前谢谢 我是这样做的:
System.import('path/to/MyComponent')
.then(fileContents => fileContents['MyComponent'])
.then(component => {
let factory = this.componentFactoryResolver.resolveComponentFactory(component);
this.container.createComponent(factory); //container: ViewContainerRef
});
我是这样做的:
System.import('path/to/MyComponent')
.then(fileContents => fileContents['MyComponent'])
.then(component => {
let factory = this.componentFactoryResolver.resolveComponentFactory(component);
this.container.createComponent(factory); //container: ViewContainerRef
});
检查此检查此检查我当前收到此错误:组件xxx不是任何NgModule的一部分,或者该模块尚未导入到您的模块中。有什么想法吗?@Gerardlamo:将动态加载的组件添加到NgModule的entryComponents列表中。我目前收到了以下错误:组件xxx不是任何NgModule的一部分,或者该模块尚未导入到您的模块中。有什么想法吗?@Gerardlamo:将动态加载的组件添加到NgModule的entryComponents列表中。