2017-01-26 69 views
0

我的代碼是這樣的:如何在vue.js 2中添加條件?

<template> 
    <a href="javascript:" class="btn btn-block btn-success" @click="addFavoriteStore($event)"> 
     <span class="fa fa-heart"></span>&nbsp;<label id="favoriteId">{{ store_id == $store->id ? 'Un-Favorite' : 'Favorite' }}</label> 
    </a> 
</template> 

<script> 
    export default{ 
     props:['idStore'], 
     mounted(){ 
      this.checkFavoriteStore() 
     }, 
     methods:{ 
      addFavoriteStore(event){ 
       var label = $('#favoriteId'); 
       var text = label.text(); 

       event.target.disabled = true 
       const payload= {id_store: this.idStore} 

       if(text == "Favorite") { 
        this.$store.dispatch('addFavoriteStore', payload) 
       } 
       else { 
        this.$store.dispatch('deleteFavoriteStore', payload)       
       } 

       setTimeout(function() { 
        location.reload(true) 
       }, 1500); 
      }, 
      checkFavoriteStore(){ 
       const payload= {id_store: this.idStore} 
       this.$store.dispatch('checkFavoriteStore', payload) 
       // this is response. return store_id 
      } 
     }, 
     data: { 
      store_id: '' 
     } 
    } 
</script> 

我對上面

你可以看一下該方法addFavoriteStore

不管這一步是正確的,因爲所描述的條件是什麼?

如何確定它是最喜歡的標籤還是不創造條件?

UPDATE

在控制檯中存在的錯誤是這樣的:

[Vue warn]: The "data" option should be a function that returns a per-instance value in component definitions. 
+0

你得到什麼樣的錯誤或嘗試,你能解釋多一點?而不是'event.target.disabled = true'爲什麼不在click方法'@ click.prevent =「」''https://vuejs.org/v2/guide/events.html :-)上添加'prevent'調用 –

+0

你的問題不是很清楚,所以請考慮改進它。你想實現什麼?實際問題在哪裏? –

+0

@Simon Davies,我更新了我的問題 –

回答

3

由於錯誤說,當你正在處理的組件,數據應該被定義爲函數返回一個對象的 - 這樣:

data() { 
    return { 
    store_id: '' 
    } 
} 
+0

當我點擊按鈕最愛,在控制檯存在這樣的錯誤:https://postimg.org/image/intx89aer/ –

+0

這是錯誤相關到後端 - 基本上你沒有發送正確的數據到enpoint或錯誤說無道理 - 所以如果你有任何形式的應用程序確認,請確保您已登錄 –

+0

在Firefox瀏覽器中,沒有錯誤。但瀏覽器chrome中有錯誤 –

1

您的數據應設置爲:

data() { 
    return { 
     store_id: '' 
    } 
    } 

作爲其組件?嘗試這個?

+0

這不正確的例子 - 數據仍然對象,它應該是功能。 –

+0

感謝衝了它,忘了():-)謝謝 –

+0

@Simon戴維斯,當我點擊按鈕最愛,在控制檯存在這樣的錯誤:https://postimg.org/image/intx89aer/ –