我的Vue和组件数据功能有问题。即使所有引用的prop数据都正确地传递给组件,我也可以在vue-devtools中看到它们。我从Laravel传递数据。
Vue错误:
[Vue warn]: Error in data(): "TypeError: Cannot read property 'isFavorited' of undefined"
found in
---> <FavBtn> at /Applications/MAMP/htdocs/decimacen.cz/resources/assets/js/components/FavBtn.vue
<Root>
FavBtn组件:
<template>
<a href="#" class="btn-link text-danger" v-on:click="ToggleFav">
<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 {
name: 'FavBtn',
props: ['fav'],
data(){
return{
isFavorited: this.fav.isFavorited,
favoritable_id: this.fav.favoritable_id,
}
},
methods: {
ToggleFav: function () {
this.AjaxRequest();
this.ToggleDOM();
},
ToggleDOM: function () {
this.isFavorited = !(this.isFavorited);
},
AjaxRequest: function () {
if (this.isFavorited)
{
axios.delete('favorites/' + this.favoritable_id)
}
else {
axios.post('favorites/' + this.favoritable_id);
}
}
}
}
</script>
以下是我在视图中使用compnent的方法:
<fav-btn v-bind:fav="{{ $user_store }}"></fav-btn>
我想知道的是我的代码中是否有任何错误。感谢。
修改
来源:
<fav-btn v-bind:fav="{"favoritable_id":3,"favoritable_type":"App\\Store","created_at":"2017-05-24 16:12:33","updated_at":"2017-05-24 16:12:33","deleted_at":null,"isFavorited":true,"pivot":{"user_id":1,"favoritable_id":3},"users":[{"id":1,"email":"hagen@wagen.cz","permissions":[],"last_login":"2017-05-25 13:38:38","name":"Franta Hagen","currency_id":1,"sid":"1400076495925ae8d480b95925ae8d480c3","created_at":"2017-05-24 16:02:21","updated_at":"2017-05-25 13:38:38","deleted_at":null,"pivot":{"favoritable_id":3,"user_id":1}}]}"></fav-btn>