我的组件代码(SearchResultView.vue)是这样的:
<template>
...
</template>
<script>
export default{
props:['search','category','shop'],
created(){
...
},
data(){
return{
loading:false
}
},
computed:{
list:function(){
var a = this.$store.state.product;
a = JSON.stringify(a)
a = JSON.parse(a)
console.log(a.list[12])
...
}
},
methods:{
...
}
}
</script>
当我在控制台上检查:console.log(a.list[12])
时,结果:
我想要名称的显示值
我尝试这样:
console.log(a.list[12].name)
在控制台上,存在这样的错误:
[Vue warn]: Error when rendering component at C:\xampp\htdocs\chelseashop\resources\assets\js\components\SearchResultView.vue:
Uncaught TypeError: Cannot read property 'name' of undefined
如何解决该错误?
最佳答案
因为它是计算值,所以它的值可能会在程序的生命周期中发生变化。在尝试从中获取成员之前,您应该检查以确保 a.list[12]
存在。
if (a.list[12])
console.log(a.list[12].name);
关于javascript - 如何解决: [Vue warn]: Error when rendering component on the vue. js 2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41979767/