vue_cart/src/store/products.ts

31 lines
658 B
TypeScript

import { defineStore } from 'pinia'
import { shop, product } from '../api/shop'
/**
* state定义
*/
type state = {
all: product[]
}
export const useProducts = defineStore('products', {
state: (): state => {
return {
// 商品列表
all: []
}
},
actions: {
getAllProducts () {
shop.getProducts(products => {
this.all = products
})
},
decrementProductInventory (id: number, number: number) {
const product = this.all.find(product => product.id === id) as product
product.inventory -= number
}
}
})