首页 > 解决方案 > Vuejs TypeError:无法读取未定义的属性“产品”

问题描述

我正在尝试使用这些product数据:

data() {
    return {
        sitename: 'Vue.js Pet Depot',
        product: {
            id: 1001,
            title: 'Cat Food, 251lb bag',
            description: 'A 25 pound bag of <em>irresistible</em>,' +
            'organic goodness for your cat.',
            price: 2000,
            image: require('./assets/images/product-fullsize.jpg'),
        },
        cart: [],
    }
},

使用这种方法:

methods: {
    addToCart: () => {
        console.log(this.product.title + ' was added to cart');
        // this.cart.push(this.product.id)
    }
}

这是我的模板:

<button class="default" v-on:click="addToCart">
    Add to cart
</button>

得到另一个Runtime Error

v-on 处理程序中的错误:“TypeError:无法读取未定义的属性‘产品’”

标签: vue.js

解决方案


尝试使用以下代码一次

methods: {
            addToCart: function() {
                console.log(this.product.title + ' was added to cart');
                // this.cart.push(this.product.id)
            }
        }

推荐阅读