删除 Typescript 的事件侦听器不起作用

Dil*_*ake 4 event-listener typescript cordova-plugins ionic-native ionic3

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

这是我正在使用的软件包

"@ionic-native/sms": "^4.3.0",
"@ionic-native/core": "^4.3.0",
"cordova-sms-plugin": "^0.1.11",
Run Code Online (Sandbox Code Playgroud)

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

这是我的代码

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

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

public ionViewWillLeave() {
    this.smsService.stopMessageListner();
}
Run Code Online (Sandbox Code Playgroud)

这是我服务中的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');
    });
}
Run Code Online (Sandbox Code Playgroud)

请帮我解决这个问题。这个问题浪费了我整整一周的时间。但仍然没有运气:(

===更新评论===

所有给定的链接都表明我必须在删除事件侦听器时传递完全相同的函数。就我而言,当我调用时,我正在this.resolveMessage(resolve)传递参数。所以我已经尝试了如下,但仍然没有解决。resolvedocument.addEventListener

  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');
        });
    }
    
    Run Code Online (Sandbox Code Playgroud)

recieveMessage()2.与 1相同。

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

        window.SMS.stopWatch(function() {
            console.log('Stop Watching');
        }, function() {
            console.log('Not Stop Watching');
        });
    }   
Run Code Online (Sandbox Code Playgroud)

recieveMessage()3.与 1相同。

    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');
            });
        });
    }
Run Code Online (Sandbox Code Playgroud)

recieveMessage()4.与 1相同。

    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');
            });
        };
    }
Run Code Online (Sandbox Code Playgroud)

谢谢

Kok*_*oko 5

我同意在打字稿中删除事件侦听器确实很痛苦。我在 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")
    }
}
Run Code Online (Sandbox Code Playgroud)

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