material-components - 如何取消监听 MDCDialog 事件
问题描述
我有一个听的小功能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
解决方案
找到了解决方案。
必须传递一个内部带有函数的变量。
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);
}
推荐阅读
- flutter - DateTime 对象不适用于 Dart 中的集合-if 功能
- ios - Alamofire 5.0 与 AlamofireNetworkActivityIndicator 2.4
- matplotlib - Holoviews 字体更改
- java - javac 添加类路径会破坏我的本地类编译
- flutter - 如何处理 ios 13 字体的更改?
- tensorflow - gpflow 2 中的优化:为什么设置 autograph=False?
- javascript - 使用 node.js 运行 hmtl 页面时避免登录弹出窗口
- python - 在 Python Selenium 中,如何等待找到两个元素并继续循环
- shiny - 如何在 Shiny 中使用“renderDataTable”?
- security - 带有 checkmarx 的 CakePhp 3 文件操作漏洞