简体   繁体   English

Windows 旧宽度在第一次调整大小时未定义,但从第二次调整大小时其工作正常,Vue jS

[英]Windows old width coming undefined on first resize, but from 2nd time resize its working fine, Vue jS

I am doing something on window resize in VUE, and wrote a function which will give me the window width after resize and just before resize, Ie old window size and new window size,我在 VUE 中对 window 调整大小做了一些事情,并写了一个 function 它将在调整大小之后和调整大小之前给我 window 宽度,即旧的 window 大小和新的 window 大小,

So when I am resizing the window, on very first resize its giving the old value undefined, and from 2nd resize everything is working fine, What's wrong with it that on first instance windows old value is undefined.因此,当我调整 window 的大小时,首先调整它的大小并给出未定义的旧值,从第二次调整大小开始一切正常,首先是 windows 的旧值未定义有什么问题。 while I am calculating it on load (mounted) as well.同时我也在负载(安装)上计算它。

Here is the working demo这是工作演示

TO check the issue: resize the result window and check console , on first resize it will give old value undefined, but when you resize it again, it will work fine.检查问题:调整结果 window 的大小并检查 console ,在第一次调整大小时它会给出未定义的旧值,但是当你再次调整它的大小时,它会工作正常。

methods: {
    getWindowWidth(){
          var winWidth = window.innerWidth;
          var newWinWidth = window.innerWidth;
          console.log("initial width " + winWidth);
          var doIt;
    },
    handleResize() {
      const _this = this
      //_this.mounted();
      clearTimeout(_this.getWindowWidth.doIt);
      _this.getWindowWidth.doIt = setTimeout(function() {
        _this.getWindowWidth.winWidth = _this.getWindowWidth.newWinWidth;
        _this.getWindowWidth.newWinWidth = window.innerWidth;
        console.log("new " + _this.getWindowWidth.newWinWidth);
        console.log("old " + _this.getWindowWidth.winWidth);
        //console.log("old "+ _this.winWidth);
            if(_this.getWindowWidth.newWinWidth > 1025 && _this.getWindowWidth.winWidth < 1025){
              console.log("refresh now");
              //window.location.reload();
            }
            if(_this.getWindowWidth.newWinWidth < 1025 && _this.getWindowWidth.winWidth > 1025){
              console.log("refresh again");
              //window.location.reload();
            }
        }, 1000);
      }
    },
    mounted: function() {
    const _this = this
    window.addEventListener('resize', _this.handleResize);
    }

getWindowWidth is a method/function but you are using it like it's an object, that will wok because functions in javascript are objects but the intended behavior is not what you think it is. getWindowWidth是一种方法/函数,但您像使用 object 一样使用它,它会起作用,因为 javascript 中的函数是对象,但预期行为不是您认为的那样。 When you first do getWindowWidth.winWidth that won't get the variable winWidth inside getWindowWidth , that will try to read the value of the property winWidth of the object getWindowWidth which is initially undefined .当您第一次执行getWindowWidth.winWidth时,它不会在getWindowWidth中获取变量winWidth ,它将尝试读取 object getWindowWidth的属性winWidth的值,该值最初是undefined It will obly have a value after the first time you set it, that explains why your code doesn't work the first time, because the first time is used to initialize those properties.在您第一次设置它之后,它就会有一个值,这就解释了为什么您的代码第一次不起作用,因为第一次用于初始化这些属性。

What you need to do is get rid of getWindowWidth altogether and in the mounted hook just create a regular object that holds the initial values and use that in handleResize like so:您需要做的是完全摆脱getWindowWidth并在mounted钩子中创建一个常规的 object 来保存初始值并在handleResize中使用它,如下所示:

methods: {
  handleResize() {
    clearTimeout(this.windowValues.doIt);
    this.windowValues.doIt = setTimeout(() => {         // use an arrow function here so you can refer to the same 'this' inside setTimeout and get rid of the variable '_this'
      this.windowValues.winWidth = this.windowValues.newWinWidth;
      this.windowValues.newWinWidth = window.innerWidth;
      console.log("new " + this.windowValues.newWinWidth);
      console.log("old " + this.windowValues.winWidth);
      
      if(this.windowValues.newWinWidth > 1025 && this.windowValues.winWidth < 1025) {
        console.log("refresh now");
        //window.location.reload();
      }
      if(this.windowValues.newWinWidth < 1025 && this.windowValues.winWidth > 1025) {
        console.log("refresh again");
        //window.location.reload();
      }
    }, 1000);
  }
},
mounted: function() {
  this.windowValues = {                                 // define the object 'windowValues', this object will be used in 'handleResize'
    winWidth: window.innerWidth,
    newWinWidth: window.innerWidth
    // doIt will get assigned later so no need to define it here
  };

  window.addEventListener('resize', this.handleResize);
}

Here is a working demo: https://jsfiddle.net/admqLurv/这是一个工作演示: https://jsfiddle.net/admqLurv/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM