Laravel Vue无法访问道具数据
我对Vue和组件数据函数有问题。即使所有引用的道具数据都正确地传递到组件,并且我可以在vue devtools中看到它们,它也会中断。我正在传递来自拉威尔的数据 Vue错误:Laravel Vue无法访问道具数据,laravel,vue.js,vuejs2,vue-component,Laravel,Vue.js,Vuejs2,Vue Component,我对Vue和组件数据函数有问题。即使所有引用的道具数据都正确地传递到组件,并且我可以在vue devtools中看到它们,它也会中断。我正在传递来自拉威尔的数据 Vue错误: [Vue warn]: Error in data(): "TypeError: Cannot read property 'isFavorited' of undefined" found in ---> <FavBtn> at /Applications/MAMP/htdocs/decimacen
[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>
[Vue warn]:数据()中的错误:“TypeError:无法读取未定义的属性'isFavorited'”
发现于
--->at/Applications/MAMP/htdocs/decimacen.cz/resources/assets/js/components/FavBtn.vue
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>
导出默认值{
名称:“FavBtn”,
道具:['fav'],
数据(){
返回{
我喜欢这个,
favoritable\u id:this.fav.favoritable\u id,
}
},
方法:{
ToggleFav:函数(){
这个.AjaxRequest();
this.ToggleDOM();
},
ToggleDOM:function(){
this.isFavorited=!(this.isFavorited);
},
AjaxRequest:函数(){
如果(此.isFavorited)
{
axios.delete('favorites/'+this.favoritable_id)
}
否则{
axios.post('favorites/'+this.favoritable_id);
}
}
}
}
以下是我如何使用视图中的组件:
<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>
$user\u store是一个laravel变量吗?@Bert是的。它呈现了什么?很可能是时间问题:最初,道具是空的。在分配它之前检查它是否存在:isFavorited:this.fav&&this.fav.isFavorited
(对于favoritable\u id也是如此)@RoyJ我按照您对isFavorited的建议分配了数据,并对favoritable_id进行了相同的分配,但得到了相同的错误。$user_store是一个laravel变量吗?@Bert是的。它呈现了什么?很可能是一个时间问题:最初,道具为空。在分配它之前检查它是否存在:isFavorited:this.fav&&this.fav.isFavorited
(对于favoritable\u id也是如此)@RoyJ我按照您为isFavorited建议的方式分配了数据,并对favoritable\u id执行了相同的操作,但得到了相同的错误。