VUE の監視と計算された実行順序

次のコードはプロジェクトで使用されます

  watch: {
    
    
    list: {
    
    
      deep: true,
      immediate: true,
      handler(val) {
    
    
        if (val.length > this.num) {
    
    
          for (let i = 0, len = val.length; i < len; i += this.num) {
    
    
            this.everyList.push(val.slice(i, i + this.num));
          }
        } else {
    
    
         // 这句话 和 下边的 计算属性都用到了ref =  mySwiper;
         // 我在这打印console.log(this.$refs.mySwiper)  输出 undefined,导致了报错
          this.$refs.mySwiper.swiper.autoplay.start();
          this.everyList = [val];
        }
      },
    },
  },
  computed: {
    
    
    swiper() {
    
    
      return this.$refs.mySwiper.swiper;
    },
 },

その後、watchの実行順序の問題だと思い計算してみました。

実行順序の初期化

  • watchがimmediate:trueに設定されている場合
  • ウォッチが最初に実行されてからマウントされます

この値が変更された場合、この値が監視され、計算されるタイミングもこの値に依存します。

  • 監視が最初に実行される
  • 計算後に実行

Guess you like

Origin blog.csdn.net/pink_cz/article/details/126148830