VUE.JS 窗口发生变化时,获取当前窗口的高度。

VUE.JS

# 窗口发生变化时,获取当前窗口的高度。

方案一:

  mounted () {
    const that = this;
    window.onresize = () => {
      return (() => {
        that.screenHeight = window.innerHeight
      })()
    }
  },
  data(){
    return {
      screenHeight: window.innerHeight,
    }
  }

 方案二:

export default {
   data(){
     return {
       screenHeight: window.innerHeight,
     }
   },
    mounted() {
        window.addEventListener('resize', this.onResize);
    },
    beforeDestroy() {
        window.removeEventListener("resize", this.onResize);
    },
    methods: {
        onResize() {
            this.screenHeight = window.innerHeight;
        }
    }
}

 

posted @ 2018-11-04 22:33  未来的羁绊  阅读(12643)  评论(0编辑  收藏  举报