Vue 實時監(jiān)聽窗口變化 windowresize的兩種方法
下面給大家分享兩種方法來介紹Vue 實時監(jiān)聽窗口變化 windowresize,具體內(nèi)容如下所示:
方法一:
First-step : 定義變量
data(){
return{
formLabelWidth : '123px'
}
},
Second-step: 根據(jù)生命周期 在mounted 中綁定 窗口變化
mounted(){
const that = this
window.onresize = () => {
return (() => {
window.screenWidth = document.body.clientWidth
that.screenWidth = window.screenWidth
})()
}
},
Third-step: 綁定監(jiān)聽 watch
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.2x里面時候,mounted 里面可以直接掛載 window.onresize事件。全局監(jiān)聽
mounted(){
window.onresize = () => {
return (() => {
this.handleLableWidth();
})()
}
this.handleLableWidth();
},
完全可以做到檢測窗口變化
總結(jié)
以上所述是小編給大家介紹的Vue 實時監(jiān)聽窗口變化 windowresize的兩種方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
詳解基于webpack和vue.js搭建開發(fā)環(huán)境
本篇文章主要介紹了詳解基于webpack和vue.js搭建開發(fā)環(huán)境 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
關(guān)于ElementUI自定義Table支持render
這篇文章主要介紹了關(guān)于ElementUI自定義Table支持render,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10

