2017-10-07 44 views
-1

我想應用jquery選擇器類「錯誤」時,如果條件變爲false,我如何可以聽V - 如果更改。如何檢測V - 如果內容已更改

<template> 
 
    <div class="view1"> 
 
    <div v-if="ok"> 
 
    <p class="right">OK</p> 
 
    </div> 
 
    <div v-else> 
 
    <p class="wrong">NO</p> 
 
    </div> 
 
    </div> 
 
</template> 
 
<script> 
 
    export default { 
 
    mounted(){ 
 
     $('.wrong').animate({width:'20%'},2000); 
 
    } 
 
    } 
 
</script>

+1

你應該把你的jQuery邏輯到'mounted' VUE掛鉤,但我真的不建議將jQuery這裏涉及。 –

+0

你能展示使用jQuery代碼嗎? –

回答

1

你可以用CSS,而不是jQuery的做到這一點。訣竅是有一個寬度過渡,並在應用寬度風格延遲。您通過使用watch來聆聽更改。

new Vue({ 
 
    el: '.view1', 
 
    data: { 
 
    ok: true, 
 
    delayedReaction: null 
 
    }, 
 
    methods: { 
 
    toggle() { 
 
     this.ok = !this.ok; 
 
    } 
 
    }, 
 
    watch: { 
 
    ok() { 
 
     if (this.ok) { 
 
     this.delayedReaction = null; 
 
     } else { 
 
     // nextTick didn't suffice 
 
     setTimeout(() => { 
 
      this.delayedReaction = { 
 
      width: '20%' 
 
      }; 
 
     }, 0); 
 
     } 
 
    } 
 
    } 
 
});
.wrong { 
 
    background-color: #fdd; 
 
    transition: width 2s; 
 
    width: 100%; 
 
}
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script> 
 
<div class="view1"> 
 
    <div v-if="ok"> 
 
    <p class="right">OK</p> 
 
    </div> 
 
    <div v-else> 
 
    <p class="wrong" :style="delayedReaction">NO</p> 
 
    </div> 
 
    <button @click="toggle">Toggle</button> 
 
</div>

+0

好的,我會用手錶....非常感謝 –

相關問題