使用vue,将商品详情页的数据存储到pinia,购物车页面读取pinia里的数据并显示

时间: 2023-10-25 13:11:47 浏览: 40
首先,需要安装 Pinia: ``` npm install pinia ``` 然后,在 Vue 3 的入口文件中引入和创建 Pinia 实例: ```javascript import { createApp } from 'vue' import { createPinia } from 'pinia' import App from './App.vue' const app = createApp(App) // 创建 Pinia 实例 const pinia = createPinia() // 将 Pinia 实例挂载到应用上 app.use(pinia) // 启动应用 app.mount('#app') ``` 接下来,我们在 Pinia 中定义 `ProductStore` 和 `CartStore`: ```javascript import { defineStore } from 'pinia' export const ProductStore = defineStore('product', { state: () => ({ products: [] }), actions: { fetchProducts() { // 异步获取商品数据 // ... // 获取到商品数据后,将数据存储到 state 中 this.products = [ /* 商品数据 */ ] } } }) export const CartStore = defineStore('cart', { state: () => ({ cartItems: [] }), actions: { addItemToCart(item) { // 添加商品到购物车 // ... // 添加成功后,将购物车数据更新到 state 中 this.cartItems.push(item) } } }) ``` 在商品详情页中,我们需要获取商品数据并将数据存储到 `ProductStore` 中: ```html <template> <div> <h1>{{ product.name }}</h1> <p>{{ product.description }}</p> <button @click="addToCart">加入购物车</button> </div> </template> <script> import { defineComponent, computed } from 'vue' import { useRoute } from 'vue-router' import { useStore } from 'pinia' import { ProductStore } from '@/stores' export default defineComponent({ setup() { const route = useRoute() const store = useStore(ProductStore) const productId = route.params.id // 获取商品数据 store.fetchProducts() // 根据商品 ID 获取商品详情 const product = computed(() => { return store.products.find(item => item.id === productId) }) // 添加商品到购物车 const addToCart = () => { const item = { id: product.value.id, name: product.value.name, price: product.value.price, quantity: 1 } store.cart.addItemToCart(item) } return { product, addToCart } } }) </script> ``` 在购物车页面中,我们需要读取 `CartStore` 中的数据并显示: ```html <template> <div> <h1>购物车</h1> <ul> <li v-for="item in cartItems" :key="item.id"> <p>{{ item.name }}</p> <p>{{ item.price }}</p> <p>{{ item.quantity }}</p> </li> </ul> </div> </template> <script> import { defineComponent } from 'vue' import { useStore } from 'pinia' import { CartStore } from '@/stores' export default defineComponent({ setup() { const store = useStore(CartStore) // 获取购物车数据 const cartItems = store.cartItems return { cartItems } } }) </script> ``` 最后,我们需要在路由中定义商品详情页和购物车页面的路由: ```javascript import { createRouter, createWebHistory } from 'vue-router' import ProductDetail from '@/views/ProductDetail.vue' import ShoppingCart from '@/views/ShoppingCart.vue' const routes = [ { path: '/product/:id', name: 'ProductDetail', component: ProductDetail }, { path: '/shopping-cart', name: 'ShoppingCart', component: ShoppingCart } ] const router = createRouter({ history: createWebHistory(), routes }) export default router ``` 这样,我们就完成了将商品详情页的数据存储到 Pinia,购物车页面读取 Pinia 里的数据并显示的流程。

相关推荐

最新推荐

recommend-type

Vue使用localStorage存储数据的方法

本文实例为大家分享了Vue使用localStorage存储数据的具体代码,供大家参考,具体内容如下 通过下面这个案例来了解localStorage的基本使用方法。 输入评论人、评论内容,点击发表评论,评论数据将保存到localStorage...
recommend-type

在Vue里如何把网页的数据导出到Excel的方法

主要介绍了在Vue里如何把网页的数据导出到Excel,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

Vue下滚动到页面底部无限加载数据的示例代码

本篇文章主要介绍了Vue下滚动到页面底部无限加载数据的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

vue数据更新UI不刷新显示的解决办法

vue比较常见的坑就是数据(后台返回)更新了,但是UI界面并没有更新,常见于以下情况: 一、数据为数组时 1.通过数组索引修改数组元素例如: 此时UI数据并不会刷新 2.修改数组长度时: 解决方案: 如果data为JSON...
recommend-type

vue实现购物车功能(商品分类)

本文实例为大家分享了vue实现购物车功能的具体代码,供大家参考,具体内容如下 new Vue({ el: "#app", data: { cIndex: 0, lists: [ { title: "推荐商品", goods: [ { id: 0, img: './images/goods.png',...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

深入了解MATLAB开根号的最新研究和应用:获取开根号领域的最新动态

![matlab开根号](https://www.mathworks.com/discovery/image-segmentation/_jcr_content/mainParsys3/discoverysubsection_1185333930/mainParsys3/image_copy.adapt.full.medium.jpg/1712813808277.jpg) # 1. MATLAB开根号的理论基础 开根号运算在数学和科学计算中无处不在。在MATLAB中,开根号可以通过多种函数实现,包括`sqrt()`和`nthroot()`。`sqrt()`函数用于计算正实数的平方根,而`nt
recommend-type

react的函数组件的使用

React 的函数组件是一种简单的组件类型,用于定义无状态或者只读组件。 它们通常接受一个 props 对象作为参数并返回一个 React 元素。 函数组件的优点是代码简洁、易于测试和重用,并且它们使 React 应用程序的性能更加出色。 您可以使用函数组件来呈现简单的 UI 组件,例如按钮、菜单、标签或其他部件。 您还可以将它们与 React 中的其他组件类型(如类组件或 Hooks)结合使用,以实现更复杂的 UI 交互和功能。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。