我的代碼是這樣的:如何在vue.js 2中添加條件?
<template>
<a href="javascript:" class="btn btn-block btn-success" @click="addFavoriteStore($event)">
<span class="fa fa-heart"></span> <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.
你得到什麼樣的錯誤或嘗試,你能解釋多一點?而不是'event.target.disabled = true'爲什麼不在click方法'@ click.prevent =「」''https://vuejs.org/v2/guide/events.html :-)上添加'prevent'調用 –
你的問題不是很清楚,所以請考慮改進它。你想實現什麼?實際問題在哪裏? –
@Simon Davies,我更新了我的問題 –