【发布时间】:2017-10-17 06:39:30
【问题描述】:
我在访问 Vue 组件中的数据时遇到问题,我使用 prop 将数据从视图传递到组件,就像这样。我正在使用 Laravel。
<fav-btn v-bind:store="{{ $store }}"></fav-btn>
我的组件看起来像这样:
<template>
<a href="#" class="btn-link text-danger" v-on:click="favorite">
<i v-bind:class="{ 'fa fa-heart fa-2x': isFavorited == true, 'fa fa-heart-o fa-2x': isFavorited == false }" class="" aria-hidden="true"></i>
</a>
</template>
<script>
export default {
props: ['store'],
data(){
return{
isFavorited: this.store.favoritable.isFavorited,
}
},
methods: {
favorite: function () {
this.AjaxRequest();
this.ToggleFav();
},
ToggleFav: function () {
this.isFavorited = !(this.isFavorited);
},
AjaxRequest: function () {
if (this.isFavorited)
{
axios.delete('stores/' + this.store.favoritable_id);
}
else {
axios.post('stores/' + this.store.favoritable_id);
}
}
}
}
</script>
在 Vue devtools 中,我可以看到 props 中的所有对象,但我无法访问它们,isFavorited 始终保持为 false。我是否错误地访问了对象属性?
【问题讨论】:
-
组件中
store的值是多少?你确定它正确通过了吗? -
让你
isFavorited在计算属性中。 -
我发现了问题。该组件根本不会在浏览器中刷新,我不知道如何修复它。但是今天我从睡眠中唤醒了计算机,并且更改已应用于浏览器...