我的场景如下:我有3个组件: MainComponent、ComponentA、ComponentB
MainComponent是动态负荷ComponentA。ComponentA有一个onClick称为MainComponent.addComponent(ComponentB)的按钮
export class MainLayout
{
constructor(private dcl: DynamicComponentLoader,private elementRef:ElementRef) {
///this will work fine
this.dcl.loadIntoLocation(ComponentA,elementRef);
}
addComponent(component:Type) {
///this will fail
this.dcl.loadIntoLocation(component, this.elememtRef,'child1');
}
}我所得到的错误:您可以看到错误来自于elementRef对象.
未定义异常:在计算“单击”原始异常时出错: TypeError:无法读取未定义原始STACKTRACE的属性'_view‘:TypeError:无法读取未定义的Object.internalView (modules/angular2/bundles/angular2.dev.js:6236:19) at DynamicComponentLoader_.loadIntoLocation (modules/angular2/bundles/angular2.dev.js:11240:33) at MainLayout.resolveComponent (http://localhost:63342/decisionApp/decision-modeling-ui/app/playground/ui-composition/mainLayout.js:32:18) at ComponentsList.onClick (http://localhost:63342/decisionApp/decision-modeling-ui/app/playground/ui-composition/ComponentsList.js:25:20)的属性'_view’。
发布于 2016-01-11 11:32:26
好的,我知道了,我之所以有不同的objectRefs,是因为我在ComponenetA配置中设置了一个提供程序:MainComponent。这为我创建了一个新的MainComponent实例。因此,在构造函数中注入了一个新的ElementRef实例。
发布于 2016-01-11 09:50:50
您的描述不太清楚,因为您没有告诉我们有关组件的失败消息和其他一些事情。所以我只能猜测:
某个组件,this.elememtRef,'child1‘在您调用addComponent时不存在,所以它会失败吗?也许ComponentA还没有完全加载呢?我还假设"component“是指ComponentB?
https://stackoverflow.com/questions/34717989
复制相似问题