简体   繁体   English

如何在vue.js 2中显示对象的值?

[英]How to display value of objects in vue.js 2?

My code is like this : 我的代码是这样的:

<script>
    export default {
        props:['search','category','shop'],
        created(){
            ...
        },
        data(){
            return{
                loading:false
            }
        },
        computed:{
            ...
            list:function(){
                console.log(this.$store.state.product);
                return this.$store.state.product.list
            },
        },
        methods:{
            ...
        }
    }
</script>

I do : console.log(this.$store.state.product); 我做了: console.log(this.$store.state.product); in list method 列表方法

Then, I check it on the console 然后,我在控制台上检查

The result is like this : 结果是这样的:

在此处输入图片说明

I want display value of name 我想要显示名称的值

I try like this : 我这样尝试:

console.log(this.$store.state.product.list.id.name);

There exist error : 存在错误:

Uncaught TypeError: Cannot read property 'name' of undefined 未捕获的TypeError:无法读取未定义的属性“名称”

How can I solve the error? 我该如何解决错误?

The list is an object, with the keys being the different element id's - you are currently trying to access like this: list是一个对象,键是不同元素的ID-您当前正在尝试像这样访问:

this.$store.state.product.list.id.name

You are getting that error because there is no id key in the list object, you need to replace .id with the actual id value, like this: 因为列表对象中没有id键,所以出现了此错误,您需要将.id替换为实际的id值,如下所示:

this.$store.state.product.list["12"].name; //"Bunga Gandeng"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM