这篇文章主要为大家详细介绍了浅谈VUE监听窗口变化事件的问题,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!
Vuejs 本身就是一个 MVVM 的框架。但是在监听 window 上的 事件 时,往往会显得 力不从心。
比如 这次是 window.resize恩,我做之前也是百度了一下。看到大家伙都为这个问题而发愁。
代码如下:
data: {
screenWidth: document.body.clientWidth // 这里是给到了一个默认值 (这个很重要)
}
代码如下:
mounted () {
const that = this window.onresize = function() {
return function(){
window.screenWidth = document.body.clientWidth;
that.screenWidth = window.screenWidth
})()
}
}
代码如下:
watch: {
screenWidth (val) {
this.screenWidth = val
}
}
代码如下:
watch: {
screenWidth (val) {
if (!this.timer) {
this.screenWidth = val
this.timer = true
let that = this
setTimeout(function () {
// that.screenWidth = that.$store.state.canvasWidth
console.log(that.screenWidth)
that.init()
that.timer = false
}, 400)
}
}
}
以上这篇浅谈VUE监听窗口变化事件的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持四海网。
本文来自:http://www.q1010.com/184/4561-0.html
注:关于浅谈VUE监听窗口变化事件的问题的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:vue.js
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。