如果是目录组件、产品模块和类似于购物车的东西。产品模块存储所有产品的列表,订单模块存储已购买产品的Id和数量。如何确定特定产品的“购买”按钮已被点击,显示有关它的消息并使按钮无效(禁用)?对于代码的任何评论,业力的巨大加分,谢谢!
目录.vue
<template>
<b-row>
<b-col sm="6" lg="4" xl="3" v-for="product in products" :key="product.id">
<div class="image" v-lazy:background-image="product.image">
Видно если нажали "Купить".
</div>
<div class="d-flex justify-content-between">
<div>
<p class="mb-0">{{product.name}}</p>
<p class="mb-0">{{product.price}} грн.</p>
</div>
<b-button class="align-self-center" variant="outline-dark" @click="buyProduct(product.id)">Купить</b-button>
</div>
</b-col>
</b-row>
</template>
<script>
import { BRow, BCol, BButton } from 'bootstrap-vue'
import { mapState, mapActions } from 'vuex'
export default {
components: {
BRow,
BCol,
BButton
},
computed: mapState({
products: state => state.product.all
}),
methods: {
...mapActions('order', ['buyProduct'])
}
}
</script>
产品.js
import axios from 'axios'
export default {
namespaced: true,
strict: true,
state: {
page: 1,
all: []
},
mutations: {
incrementPage: (state) => {
state.page++
},
addToAllProducts: (state, products) => {
state.all.push(...products)
}
},
actions: {
getFourProducts: (context, $state) => {
axios.get('http://api.local/api/products', {
params: {
page: context.state.page
},
}).then(({ data }) => {
if (data.data.length) {
context.commit('incrementPage')
context.commit('addToAllProducts', data.data)
$state.loaded()
} else {
$state.complete()
}
})
}
}
}
order.js
export default {
namespaced: true,
strict: true,
state: {
all: []
},
mutations: {
addToOrder(state, id) {
state.all.push({
id,
quantity: 1
})
}
},
actions: {
buyProduct(context, id) {
context.commit('addToOrder', id)
}
}
}
我想这就够了。没查,因为 你需要收集一个沙盒,但你没有提供你的。注意
checkOrder(product.id)我添加的方法。