【问题标题】:How to unlisten MDCDialog event如何取消监听 MDCDialog 事件
【发布时间】:2020-01-13 12:46:35
【问题描述】:

我有一个听MDCDialog:closing的小功能。

问题是,每次我运行这个函数时,它都会添加一个新的监听器。
所以,一旦我用完这个监听器,我就需要删除它。

到目前为止,这就是我所做的:

function confirm() {
    mdcAlert.open();
    // start listening
    mdcAlert.listen("MDCDialog:closing", function(event) {
        {... execute what need to be done ...}

        // stop listening (not working)
        mdcAlert.unlisten("MDCDialog:closing");
    });
}

你知道如何使用unlisten吗?

我不知道如何在文档中使用它:
https://material.io/develop/web/components/dialogs/
https://pub.dev/documentation/mdc_web/latest/mdc_web/MDCComponent/unlisten.html

【问题讨论】:

  • 你能检查是否已经有一个监听器,如果它不存在则只添加一个?
  • 对不起@JimJimson,当我找到解决方案时忘记发布我的回复。刚刚发布。

标签: material-components material-components-web


【解决方案1】:

找到了解决办法。

必须传递一个内部有函数的变量。

function confirm() {
    let eventListener=function(event) {
        {... execute what need to be done ...}

        //Unlisten after execution
        mdcAlert.unlisten("MDCDialog:closing", eventListener);
    };

    mdcAlert.open();
    mdcAlert.listen("MDCDialog:closing", eventListener);
}

【讨论】:

    猜你喜欢
    • 2021-06-12
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 2022-01-14
    • 2019-08-12
    • 1970-01-01
    • 2015-07-19
    相关资源
    最近更新 更多