vue 中获取屏幕尺寸

data() {
      return {
        screenWidth: document.body.clientWidth, // 屏幕尺寸
      }
    },
// 钩子函数
mounted () {
      const that = this
      window.onresize = () => {
        return (() => {
          window.screenWidth = document.body.clientWidth
          that.screenWidth = window.screenWidth
        })()
      }
    }
window.onresize

是window的事件,只能绑定一次,对于VUE单页面来说 最好这个写在APP页面,这样只要这个事件触发,就可以通过this.$refs找到其他组件,触发其中的 适应屏幕的内容

猜你喜欢

转载自blog.csdn.net/zcy_csdn123/article/details/80929809