Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了typescript – Angular 2动态加载组件,事件没有回击?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用了Angular2动态组件加载器.一切都运作良好.

我动态加载的组件发出事件,但我无法在任何地方捕获它.

家长代码

this.dcl.loadAsRoot(someComponent,"#dynamiccomponenthere",this.injector)

父模板:

<div id="dynamiccomponenthere" (someCustomevent)="someFunc()"></div>

儿童:

...
this@L_975_1@mecustomevent.emit(data)
...

*解决方案:( thx Gunther)*

cmp.instance['someCustomevent'].subscribe(ev => {
    this.consoleLog(ev) // run function in parent!
})

解决方法

> LoadAsRoot不会调用更改检测

目前,loadAsRoot()仅用于引导根组件(AppComponent),并且根组件@L_618_5@输入.

此注释https://github.com/angular/angular/issues/6370#issuecomment-193896657中解释了一种解决方法

function onYourComponentDispose() {
}
let el = this.elementRef
let reuseInjectorOrCreateNewOne = this.injector;
this.componentLoader.loadAsRoot(YourComponent,this.elementRef,reuseInjectorOrCreateNewOne,onYourComponentDisposE)
.then((compRef: ComponentRef) => {
  // manually include Inputs
  compRef.instance['myInputValue'] = {res: 'randomDataFromParent'};
  // manually include Outputs
  compRef.instance['myOutputValue'].subscribe(this.parentObserver)
  // trigger change detection
  cmpRef.LOCATIOn.internalElement.parentView.changeDetector.ref.detectChanges()
  // always return in a promise
  return compRef
});

另见@ContentChild is null for DynamicComponentLoader

大佬总结

以上是大佬教程为你收集整理的typescript – Angular 2动态加载组件,事件没有回击?全部内容,希望文章能够帮你解决typescript – Angular 2动态加载组件,事件没有回击?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。