2017-08-09 51 views
0

我有這個div:v-on中調用的兩個函數:單擊。在啓動另一個之前等待渲染完成?

<div 
    class="item parent-track" 
    v-on:click="expandTrack(jsonObject.tracks.indexOf(track));setGrey();" 
></div> 

的方法expandTrack創建DIV中動態新項目,我要啓動的方法setGrey(適用於所有的模板)渲染結束後。

看來,這不是發生了什麼事情。 expandTrack方法啓動,但setGrey不適用於已創建的項目。

+1

https://vuejs.org/v2/api/#Vue-nextTick – thanksd

+0

謝謝,謝謝:) –

回答

0

使用Vue.nextTick()當您需要等待DOM完成呈現之前執行代碼。這個方法可以通過vm.$nextTick在每個Vue實例上使用。

在你的情況下,它可能是這樣的:

<div class="item parent-track" v-on:click="foo"> 
methods: { 
    foo() { 
    this.expandTrack(this.jsonObject.tracks.indexOf(this.track)); 
    this.$nextTick(this.setGrey); 
    } 
} 

或者,如果你真的想所有的代碼在你的模板,你可以這樣做:

<div 
    class="item parent-track" 
    v-on:click="expandTrack(jsonObject.tracks.indexOf(track)); $nextTick(setGrey);" 
></div> 
相關問題