vue如何取消监听

lewis 2019-03-25 19次阅读

在vue中取消监听的方法:1.新建项目,引入vue;2.使用window.addEventListener方法创建监听;3.使用window.removeEventListener方法取消监听;

具体步骤如下:

1.首先,新建一个html项目,并在项目中引入vue;

import Vue from 'vue'

2.引入vue后,在项目中使用window.addEventListener方法创建监听;

mounted() {

window.addEventListener("scroll", this.setHeadPosition, true);

},

3.最后,监听创建好后,使用window.removeEventListener方法即可取消监听;

destroyed() {

window.removeEventListener("scroll", this.setHeadPosition, true);

},



发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。