在窗口上添加Vue.js事件 [英] Add Vue.js event on window

查看:106
本文介绍了在窗口上添加Vue.js事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Vue.js允许在元素上应用事件:

Vue.js allow apply event on element:

<div id="app">
   <button @click="play()">Play</button>
</div>

但如何在窗口上应用事件对象?它不在DOM中。

But how to apply event on window object? it is not in DOM.

例如:

<div id="app">
  <div @mousedown="startDrag()" @mousemove="move($event)">Drag me</div>
</div>

在这个例子中,如何在窗口上监听mousemove事件

in this example, how to listen mousemove event on window ?

推荐答案

您应该在创建和销毁组件时手动执行此操作

You should just do it manually during the creation and destruction of the component

...
created: function() {
  window.addEventListener('mousemove',this.move);
},
destroyed: function() {
  window.removeEventListener('mousemove', this.move);
}
...

这篇关于在窗口上添加Vue.js事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆