vue.js - 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:无法读取未定义的属性‘产品’”
解决方案
尝试使用以下代码一次
methods: {
addToCart: function() {
console.log(this.product.title + ' was added to cart');
// this.cart.push(this.product.id)
}
}
推荐阅读
- javascript - 单击按钮时调用 gtag_report_conversion
- python - 并行 K-Means 的 MPI 收集不适用于 2 个或更多处理器
- python - 如何在 Python 中从突出显示中计算字符串
- power-automate - 电源自动化。一次审批多行
- command-line - 提取文件并重命名每个 .rar 中的文件之一
- docker - 在 Docker 中自动启动 Headless Chromium
- assets - 尝试使用“com.day.cq.dam.core.impl.collection.DamCollectionManager;”创建空资产集合
- javascript - React Native VirtualizedList 从对象中提取键
- listview - 在颤动中实现带有文本的可缩放列表视图
- python - python-使用os.walk查找文件名中值最高的文件