vue 添加移除beforeunload事件

Posted bearwcn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vue 添加移除beforeunload事件相关的知识,希望对你有一定的参考价值。

 var vue = new Vue({
        el: ‘#app‘,
        methods: {
            //添加beforeunload监听事件
            createBeforeunloadHandler() {
                //window.addEventListener(‘beforeunload‘, e => this.beforeunloadHandler(e));
                window.addEventListener(‘beforeunload‘, this.beforeunloadHandler, false);
            },
            //移除beforeunload事件
            destroyedBeforeunloadHandler() {
                //window.removeEventListener(‘beforeunload‘, e => this.beforeunloadHandler(e));//错误方法,无法移除
                window.removeEventListener(‘beforeunload‘, this.beforeunloadHandler, false);
                //this.rowEditEnable = 0;
            },
            //beforeunload监听事件
            beforeunloadHandler(e) {
                    e.returnValue = "确定要关闭窗口吗?";
                    console.log("释放权限操作");
                    //释放权限操作,无阻塞
                    //用户点击取消后执行,恢复操作
                    setTimeout(function () {
                        setTimeout(function () {
                 console.log("恢复用户权限操作");
              }, 50) }, 50); }, }, beforeDestroy() { //在 beforeDestroy 钩子移除beforeunload事件 this.destroyedBeforeunloadHandler(); }, });

 



以上是关于vue 添加移除beforeunload事件的主要内容,如果未能解决你的问题,请参考以下文章

vue router-link 上添加点击事件

jQuery事件委托为动态添加的全部元素移除类名

vue.js怎样移除绑定的点击事件

vue项目监听滑块并移除。

vue 里 onresize 事件被覆盖,以及怎么实现 resize 防抖移除 resize 事件?

怎么移除地图监听事件