一、在data设置值记录屏幕宽度
data() {
return {
screenWidth: null,
}
},
二、监听屏幕尺寸变化
mounted() {
//获取屏幕尺寸
this.screenWidth = document.body.clientWidth
window.onresize = () => {
console.log('高度');
//屏幕尺寸变化
return (() => {
this.screenWidth = document.body.clientWidth
})()
}
},
watch: {
screenWidth: function (n, o) {
if (n <= 1200) {
console.log('屏幕宽度小于1200了')
} else {
console.log('屏幕宽度没有小于1200')
}
}
}