Angular 单击事件后ElementRef已更改
我的设想如下: 我有三个组件:主组件、组件A、组件BAngular 单击事件后ElementRef已更改,angular,Angular,我的设想如下: 我有三个组件:主组件、组件A、组件B export class MainLayout { constructor(private dcl: DynamicComponentLoader,private elementRef:ElementRef) { ///this will work fine this.dcl.loadIntoLocation(ComponentA,elementRef); } addComponent(comp
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');
}
}
主要部件是动态加载部件A。
ComponentA有一个按钮,单击调用MainComponent.addComponent(ComponentB)
我得到的错误是:
您可以看到错误来自elementRef对象
未捕获异常:计算“单击”时出错
原始异常:TypeError:无法读取未定义的属性“\u view”
原始堆栈跟踪:
TypeError:无法读取未定义的属性“\u视图”
在Object.internalView()处
在AppViewManager上。GetNameDelementIncomonentView()
在DynamicComponentLoader处。LoadInLocation()
在MainLayout.resolveComponent()处
在ComponentList.onClick()处
您的描述不是很清楚,因为您没有告诉我们有关组件的故障消息和其他一些事情。所以我只能猜测: 在调用addComponent时,组件的某个部分,这个.elememtRef,'child1'不存在,所以它会失败吗?可能ComponentA尚未完全加载?
我还假设“组件”是指组件B?您的描述不是很清楚,因为您没有告诉我们有关组件的故障消息和其他一些事情。所以我只能猜测: 在调用addComponent时,组件的某个部分,这个.elememtRef,'child1'不存在,所以它会失败吗?可能ComponentA尚未完全加载? 我还假设“component”是指ComponentB?好的,我猜出来了, 我使用不同objectRefs的原因是我在ComponenetA配置中设置了一个providers:[MainComponent]。 这为我创建了一个MainComponent的新实例。 因此在构造函数中注入了一个新的ElementRef实例。好的,我找到了它, 我使用不同objectRefs的原因是我在ComponenetA配置中设置了一个providers:[MainComponent]。 这为我创建了一个MainComponent的新实例。
因此在构造函数中注入了ElementRef的新实例。为什么要使用全局变量?你是否在这两者之间来回移动到组件?我将我的问题更改为更清楚…你是否看到你有打字错误
this.elememtRef
!=this.elementRef
为什么要使用全局变量?你是否在这两者之间来回移动到组件?我将我的问题更改为更清楚…你是否看到你有打字错误this.elememtRef
!=<代码>此.elementRef