转自简书@吴小冷
附上代码
data(){
retrun {
screenWidth:null,
dialogWidth:0,
}
},
mounted() {
this.screenWidth = document.body.clientWidth
window.onresize = () => {
return (() => {
this.screenWidth = document.body.clientWidth
})()
}
},
watch: {
screenWidth: {
handler: function (val, oldVal) {//val是新的屏幕宽度 oldval是原来的屏幕宽度
if (val < 800) {
this.dialogWidth = '400px'
//可以在这里控制侧边导航栏的收缩与展开
//此刻屏幕宽度小于800
} else {
this.dialogWidth = '800px'
//此刻屏幕宽度大于800
}
},
},
},