vue.js - Vue.js 属性或方法“addToCart”未在实例上定义
问题描述
我正在尝试制作一个组件,但它说我没有定义“addToCart”。我已经在同一个组件中定义了它,但我继续得到同样的错误?
Vue.component("solid-back",{
template: `
<div>
<h1>{{ title }}</h1>
<img :src="image">
<h3>Price</h3>
<p>{{price}}</p>
<ul id="deets">
<li v-for="detail in details">{{ detail }}</li>
</ul>
<button v-on:click="addToCart"
id="btn_add-to-cart">
Add to Cart
</button>
<button id="btn_buy-now">
Buy Now
</button>
</div>
`,
data() {
return {
title: "Solid Back Black Chair",
image: "./photos/chairs/solidblack.jpg",
price: "$34.00",
details: ["3 feet tall", "Oakwood", "8 pounds"],
}
},
method: {
addToCart() {
this.$emit("add-to-cart")
}
}
})
这是我继续收到的警告
[Vue warn]: Property or method "addToCart" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option or for class-based components, by initializing the property.
解决方案
要在 Vue 中定义方法,属性键是methods
. 看起来您的methods
属性拼写错误(单数)method
。
将其更改为methods: { ... }
应该可以工作:
methods: {
addToCart() {
this.$emit("add-to-cart")
}
}
推荐阅读
- typescript - Typescript Angular中带有索引的最后状态数组
- angular - 垫表可扩展行 Angular 7
- python - 正则表达式 findall python
- java - 如何在 RecyclerView 中选择多个项目并将它们放入 List
- html - CSS :before 出现 :after
- rest - 尝试创建超出最大限制的资源时,POST API 请求的适当 http 状态代码应该是什么
- spring - OnetoOne 双向映射不适用于 spring 和 hibernate
- hibernate - 线程“主”org.hibernate.MappingException 中的异常:
- django - 在 Django admin 中显示图像 - 多对多内联管理视图
- jenkins - 在 Jenkinsfile 中的远程主机上执行命令