为什么在定时器中冻结状态无法生效?
发布于 3 年前 作者 banyungong 1198 次浏览 来自 问答
粉丝福利 : 关注VUE中文社区公众号,回复视频领取粉丝福利

类似问题已经看过了,我和这个问题就是加了个定时器的区别,但是他的这个问题已经fixed了。

  // vue
  data(){
    return {
      obj: {
        id: 0, // this only a Number,not Object
      }
    }
  },
  mounted(){
    this.obj.id++;
    Object.freeze(this.obj);
    this.obj.id++;
    console.log(this.obj.id);

    setInterval(() => {
      this.obj.id++;
      if (this.obj.id > 2) {
        Object.freeze(this.obj);
        // console.log(Object.isFrozen(this.obj)); // 
      }
      console.log(this.obj.id)
    }, 1000);
  }

  // Console result
  // 2
  // 3
  // 4
  // 5
  // 6
  // ...
回到顶部