<template>
<div>
<table>
<tr>
<td>商品编号</td>
<td><input type="text" v-model.number="id"></td>
</tr>
<tr>
<td>商品名称</td>
<td><input type="text" v-model="title"></td>
</tr>
<tr>
<td>商品价格</td>
<td><input type="text" v-model="price"></td>
</tr>
<tr>
<td colspan="2"><button @click="addCart">加入购物车</button></td>
</tr>
</table>
<table>
<thead>
<tr>
<th>编号</th>
<th>商品名称</th>
<th>价格</th>
<th>数量</th>
<th>金额</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for = "book in books" :key="book.id">
<td>{{ book.id }}</td>
<td>{{ book.title }}</td>
<td>{{ book.price }}</td>
<td><button>删除</button>{{ book.count }}<button>+</button></td>
</tr>
</tbody>
</table>
<span>总价:0.00</span>
</div>
</template>
<script>
export default {
data() {
return {
id: null,
title: '',
price: '',
quantity: 1
}
},
computed: {
books() {
return this.$store.state.items;
}
},methons: {
addCart() {
this.$store.commit('pushItemToCart', {
id: this.id,
title: this.title,
price: this.price,
count: this.quantity
})
this.id = '';
this.title = '';
this.price = '';
}
}
}
</script>
<style>
</style>
<template>
<div>
<table>
<tr>
<td>商品编号</td>
<td><input type="text" v-model.number="id"></td>
</tr>
<tr>
<td>商品名称</td>
<td><input type="text" v-model="title"></td>
</tr>
<tr>
<td>商品价格</td>
<td><input type="text" v-model="price"></td>
</tr>
<tr>
<td colspan="2"><button @click="addCart">加入购物车</button></td>
</tr>
</table>
<table>
<thead>
<tr>
<th>编号</th>
<th>商品名称</th>
<th>价格</th>
<th>数量</th>
<th>金额</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for = "book in books" :key="book.id">
<td>{{ book.id }}</td>
<td>{{ book.title }}</td>
<td>{{ book.price }}</td>
<td>金额</td>
<td><button>-</button>{{ book.count }}<button>+</button></td>
<td><button>删除</button></td>
</tr>
</tbody>
</table>
<span>总价:0.00</span>
</div>
</template>
<script>
export default {
data() {
return {
id: null,
title: '',
price: '',
quantity: 1
}
},
computed: {
books() {
return this.$store.state.items;
}
},methons: {
addCart() {
this.$store.commit('pushItemToCart', {
id: this.id,
title: this.title,
price: this.price,
count: this.quantity
})
this.id = '';
this.title = '';
this.price = '';
}
}
}
</script>
<style>
</style>
原来是写错了methods为methons
<template>
<div>
<table>
<tr>
<td>商品编号</td>
<td><input type="text" v-model.number="id"></td>
</tr>
<tr>
<td>商品名称</td>
<td><input type="text" v-model="title"></td>
</tr>
<tr>
<td>商品价格</td>
<td><input type="text" v-model="price"></td>
</tr>
<tr>
<td colspan="2"><button @click="addCart">加入购物车</button></td>
</tr>
</table>
<table>
<thead>
<tr>
<th>编号</th>
<th>商品名称</th>
<th>价格</th>
<th>数量</th>
<th>金额</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for = "book in books" :key="book.id">
<td>{{ book.id }}</td>
<td>{{ book.title }}</td>
<td>{{ book.price }}</td>
<td>金额</td>
<td><button>-</button>{{ book.count }}<button>+</button></td>
<td><button>删除</button></td>
</tr>
</tbody>
</table>
<span>总价:0.00</span>
</div>
</template>
<script>
export default {
data() {
return {
id: null,
title: '',
price: '',
quantity: 1
}
},
computed: {
books() {
return this.$store.state.items;
}
},
methods: {
addCart() {
this.$store.commit('pushItemToCart', {
id: this.id,
title: this.title,
price: this.price,
count: this.quantity
})
this.id = '';
this.title = '';
this.price = '';
}
}
}
</script>
<style>
</style>
本文记录了在Vue.js应用中使用Vuex实现购物车功能时遇到的问题,即点击添加商品到购物车无响应。经过排查,发现是methods拼写错误导致,修正后功能正常运行。
&spm=1001.2101.3001.5002&articleId=123369937&d=1&t=3&u=406d557dc9ed4d2fb8b8c4763f6513de)
229

被折叠的 条评论
为什么被折叠?



