点击事件后ElementRef发生了变化(ElementRef changed after click event)

我的场景如下:我有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对象形成的....

未被捕获EXCEPTION:评估“click”时出错原始异常:TypeError:无法读取未定义ORIGINAL STACKTRACE的属性'_view':TypeError:无法读取Object.internalView的未定义属性'_view'( http:// localhost:63342 / decisionApp /decision-modeling-ui/node_modules/angular2/bundles/angular2.dev.js:6236:19 )在AppViewManager_.getNamedElementInComponentView( http:// localhost:63342 / decisionApp / decision-modeling-ui / node_modules / angular2 / bundles /在MainLayout上的DynamicComponentLoader_.loadIntoLocation( http:// localhost:63342 / decisionApp / decision-modeling-ui / node_modules / angular2 / bundles / angular2.dev.js:14547:62 )在ComponentsList.onClick上解析组件( http:// localhost:63342 / decisionApp / decision -modeling-ui / app / playground / ui-composition / mainLayout.js:32:18 )( http:// localhost:63342 / decisionApp / decision -modeling-ui / app / playground / ui-composition / ComponentsList.js:25:20 )

my scenario is as following: I have 3 components: MainComponent,ComponentA,ComponentB

MainComponent is dymamic loading ComponentA. ComponentA has a button that onClick call 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'); } }

The error I am getting: You can see that the error is form the elementRef object....

Uncaught EXCEPTION: Error during evaluation of "click" ORIGINAL EXCEPTION: TypeError: Cannot read property '_view' of undefined ORIGINAL STACKTRACE: TypeError: Cannot read property '_view' of undefined at Object.internalView (http://localhost:63342/decisionApp/decision-modeling-ui/node_modules/angular2/bundles/angular2.dev.js:6236:19) at AppViewManager_.getNamedElementInComponentView (http://localhost:63342/decisionApp/decision-modeling-ui/node_modules/angular2/bundles/angular2.dev.js:11240:33) at DynamicComponentLoader_.loadIntoLocation (http://localhost:63342/decisionApp/decision-modeling-ui/node_modules/angular2/bundles/angular2.dev.js:14547:62) 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)

最满意答案

你的描述不是很清楚,因为你不会告诉我们关于你的组件的失败信息和其他一些事情。 所以我只能猜测:

在你调用addComponent的时候,组件this.elememtRef,'child1'不存在,所以它会失败? 也许ComponentA尚未完全加载? 我还假设“组件”是指组件B?

Ok I figured it out, The reason I had different objectRefs is that I set a providers:[MainComponent] at the ComponenetA config. this created for me a new instance of MainComponent. and therefore a new instance of ElementRef that was injected in the constructor.

更多推荐