vue的$nextTick使用后的js代碼執行順序問題


一、問題產生背景:

  父組件已經獲得子組件實例,並能直接觸發子組件的方法,在父組件中調用了子組件的兩個方法

// 父組件調用子組件,this.picker是獲取的子組件整個實例,先調用update,再調用setSlotValue
this.picker.update();
 const pro = {
    first_char: "A",
    parent_id: "1",
    region_id: "3",
    region_name: "安徽",
    sort_order: "255",
    type: "1"
}
this.picker.setSlotValue(0, pro);
// 子組件的update、setSlotValue方法以及update中調用的translate2Value方法

// update方法
 update() {
      console.log(1);
      this.$nextTick(() => {
        this.translate2Value();
      });
}, 

// translate2Value方法
translate2Value () {
    console.log(3)
}

// setSlotValue方法
setSlotValue(slotIndex, soltVal) {
      console.log(2);
      for (let [index, slot] of this.slots[slotIndex].values.entries()) {
        if (this.isObjectValueEqual(soltVal, slot)) {
          // 這里改變了頁面dom元素樣式  
          this.ul_slots[slotIndex].style.WebkitTransform = "translate3d(0px," + -(index * this.itemHeight) + "px,0px)";
          return;
        }
      }
}   
// 因為nextTick是等待頁面dom變化時渲染完畢時才執行的,所以執行結果為:
1 2 3

// update方法去掉this.$nextTick,執行結果為:
1 3 2

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM