typescript - 删除 Typescript 的事件监听器不起作用

标签 typescript ionic3 cordova-plugins event-listener ionic-native

我在我的 ionic 3 聊天应用程序中尝试了 import { SMS } from '@ionic-native/sms';。但是 document.removeEventListener('onSMSArrive'); 不工作。

这是我正在使用的包

"@ionic-native/sms": "^4.3.0",
"@ionic-native/core": "^4.3.0",
"cordova-sms-plugin": "^0.1.11",

问题是,当我第一次访问我的页面时,它运行良好,并且在我将短信发送到手机时收到消息。 但是,如果我返回到另一个页面并返回到该页面,我会在我的函数中收到两次相同的消息。我认为事件监听器没有被删除,当我再次导航到该页面时,另一个监听器正在添加到文件。

这是我的代码

我在页面加载时添加事件监听器,并在页面卸载时删除它。

public ionViewWillEnter() {
    this.smsService.startMessageListner();
}

public ionViewWillLeave() {
    this.smsService.stopMessageListner();
}

这是我服务中的 startMessageListner()stopMessageListner() 函数。

public startMessageListner()
{
    --- some works do here ---
    this.startListner();
}

public startListner()
{
    this.recieveMessage().then((message) => {
        --- receives message here and when after 
            navigating multiple times I receives 
            multiple same messages here---
    }
}

public recieveMessage() {

    if (!window.SMS) { alert('SMS plugin not ready'); return; }

    window.SMS.startWatch(function() {
        console.log('Watching');
    }, function() {
        console.log('Not Watching');
    });

    this.promise = new Promise((resolve, reject) => {
        document.addEventListener('onSMSArrive', this.resolveMessage(resolve));
    });

    return this.promise;
}

public stopMessageListner()
{
    --- some works do here ---
    this.stopReciveMessage();
}

public stopReciveMessage()
{
    // ***This one also not works***
    // document.removeEventListener('onSMSArrive', (event)=>{
    //     window.SMS.stopWatch(function() {
    //         console.log('Stop Watching');
    //     }, function() {
    //         console.log('Not Stop Watching');
    //     });
    // });

    document.removeEventListener('onSMSArrive');

    window.SMS.stopWatch(function() {
        console.log('Stop Watching');
    }, function() {
        console.log('Not Stop Watching');
    });
}

请帮我解决这个问题。这个问题浪费了我整个星期。仍然没有运气:(

===更新评论===

所有给定的链接都说我必须在删除事件监听器时传递完全相同的函数。在我的例子中,当我调用 document.addEventListener 时,我传递了带有 resolve 参数的 this.resolveMessage(resolve)。所以我已经尝试了如下,仍然没有解决。

  1. public recieveMessage() {
    
        --same content as above--
    
        let self = this; // I added this line for safe side
        this.promise = new Promise((resolve, reject) => {
            self.resolve = resolve;
            document.addEventListener('onSMSArrive', self.resolveMessage(resolve),false);
        });
    
        return this.promise;
    }
    
    public stopReciveMessage()
    {
        document.removeEventListener('onSMSArrive', this.resolveMessage(this.resolve),false);
    
        window.SMS.stopWatch(function() {
            console.log('Stop Watching');
        }, function() {
            console.log('Not Stop Watching');
        });
    }
    

2。 对于 1 中的相同 recieveMessage()

    public stopReciveMessage()
    {
        document.removeEventListener('onSMSArrive', this.resolveMessage,false);

        window.SMS.stopWatch(function() {
            console.log('Stop Watching');
        }, function() {
            console.log('Not Stop Watching');
        });
    }   

3。 对于 1 中的相同 recieveMessage()

    public stopReciveMessage()
    {
        let self = this;
        this.promise = new Promise((resolve, reject) => {
            document.removeEventListener('onSMSArrive', self.resolveMessage(resolve),false);
            window.SMS.stopWatch(function() {
                console.log('Stop Watching');
            }, function() {
                console.log('Not Stop Watching');
            });
        });
    }

4. 对于 1 中的相同 recieveMessage()

    public stopReciveMessage()
    {
        let self = this;
        this.promise = (resolve, reject) => {
            document.removeEventListener('onSMSArrive', self.resolveMessage(resolve),false);
            window.SMS.stopWatch(function() {
                console.log('Stop Watching');
            }, function() {
                console.log('Not Stop Watching');
            });
        };
    }

谢谢

最佳答案

我同意在 typescript 中删除事件监听器真的很痛苦。我在 Vue 中是这样解决的:

class App {

    mylistener:EventListener

    created(){
        this.mylistener = (e:Event) => this.logMessage(e,"hello")
        window.addEventListener('click', this.mylistener);
    }

    destroyed(){
        window.removeEventListener("click", this.mylistener)
    }

    logMessage(e:Event, str:String){
        console.log("click event was called")
    }
}

这假设在创建和删除组件时调用了“created”和“destroyed”函数。首先检查您是否可以使用一个基本示例,如果可以,请查看如何将它应用到您的 SMS 库中 - 该库可能有其自身的问题。

关于typescript - 删除 Typescript 的事件监听器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50658727/

相关文章:

angular - 使用组件和服务创建模块

angular - Ionic 2 - 是否可以使用 poptoRoot 函数提供 navParams ?

ios - cordova 应用程序中缺少框架的 Info.plist 文件

cordova - 即使安装了插件,也会出现安装 FCM 插件的问题

typescript - 使用 @azure/Msal-browser 时,我在 ClientAuthError : state_not_found: State not found 上收到此错误消息

angular - 有没有一种 Angular2 方法可以专注于输入字段?

javascript - 在 Angular 中,如何确定事件路由?

ReactJS 属性 timerID 在类型 Clock 上不存在

css - 预定义的 CSS 类不起作用 ionic3

ionic3 - 错误 : deleted_client The OAuth client was deleted