一、問題產生背景:
父組件已經獲得子組件實例,並能直接觸發子組件的方法,在父組件中調用了子組件的兩個方法
// 父組件調用子組件,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