【问题标题】:Angular 6 Custom Element Output EventEmitterAngular 6 自定义元素输出 EventEmitter
【发布时间】:2018-11-27 16:55:10
【问题描述】:

我有一个带有输出事件发射器的元素,但我不知道如何捕获该事件。我收到此错误:

“类型'() => void'不可分配给类型'EventEmitter'。”

这是输出

  @Output() closeMarkers = new EventEmitter<string>();

这就是我将元素添加到组件的方式:

              createPopupComponentWithMessage(latitude, longitude, city) {
                    const popupEl: NgElement & WithProperties<MarkerNewComponent> = document.createElement('new-marker') as any;
                      //Set MarkerNewComponent input vars
                      popupEl.city=city;
                      popupEl.latitude=latitude;
                      popupEl.longitude=longitude;
                      popupEl.closeMarkers = this.testOutput ;
                      popupEl.addEventListener('closed', () =>  document.body.removeChild(popupEl));

                      // Add to the DOM
                      document.body.appendChild(popupEl);
                    return popupEl;
                  }

        testOutput (){
               console.log('test output');
             }

【问题讨论】:

    标签: angular custom-element


    【解决方案1】:

    在父组件中,你应该有一个子组件的标签

    例如

    父组件.HTML

    <div> myInformation</div>
    <myChild></myChild>
    

    要从您的孩子那里捕捉到事件,您必须这样做

    <div> myInformation</div>
    <myChild (closeMarkers)="myParentFunctionToHandleIt($event)" ></myChild>
    

    这里是文档:https://angular.io/api/core/EventEmitter

    EDIT : 在父组件中,可以指定事件的类型

    PARENT.COMPONENT.TS

    myParentFunctionToHandleIt(value: MyCustomType) {
    //your way to handle your event
    }
    

    【讨论】:

    • 如果这不是你想要做的,你能做一个stackblitz,这样我们就可以理解你的问题;)@John
    • 我在父组件上没有标签,因为我正在使用角度自定义元素来构建自定义传单标记
    • 所以你想在有人点击标记时通知你的应用程序?
    • 我还在我当前的项目中使用 Leaflet,将函数绑定到标记我使用函数 L.divIcon 创建我的标记并在 html 中注入一些代码(例如点击函数)我不'不知道是不是你要找的(在点击函数中你可以绑定你的 eventEmitter 调用)
    • 我用这个 popupEl.addEventListener('closeMarkers', () => { console.log('closeMarkers') });
    猜你喜欢
    • 1970-01-01
    • 2018-11-28
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    相关资源
    最近更新 更多