当前位置: 首页 > news >正文

Pinia2

一、入门案例

1、安装

npm i pinia -S

2、注册插件

//main.ts

import { createPinia } from 'pinia'

app.use(createPinia())

3、创建store/countStore.ts

import { defineStore } from "pinia";

const useCounterStore = defineStore('counterStore',{

    state(){

        return{

            count:0

        }

    },

    actions:{

        add(){

            this.count++

        }

    }

})

export default useCounterStore

4、使用App.vue

<template>

  <div>

    {{ count }}

    <button @click="add">点我加一</button>

  </div>

</template>

<script setup lang='ts'>

import useCounterStore from './store/countStore';

import { storeToRefs } from 'pinia';

const counterStore = useCounterStore()

const {count} = storeToRefs(counterStore) //storeToRefs让其成为响应式

const add = counterStore.add

</script>

二、购物车案例

1、新建一个列表的productStore.js的文件

import { defineStore } from "pinia";
import axios from 'axios'const useProductStore = defineStore('productStore',{state(){return{products:[]}},actions:{async loadDate(){let res = await axios.get("http://localhost:9000/data")this.products = res.dataconsole.log(res);}}})export default useProductStore

2、购物车的商品cartStore.js

import { defineStore } from "pinia";const useCartStore = defineStore("cartStore", {state() {return {carts: [],};},actions: {add(product) {const pro = this.carts.find((value) => {console.log(value);return product.id === value.id;});product.inventory--if (pro) {pro.quality++;} else {this.carts.push({ ...product, quality: 1 });}},},getters:{total(){return this.carts.reduce((pre,cur)=>{return pre += cur.quality * cur.price},0)}}
});
export default useCartStore;

3.使用App.vue

<template><div><h1>产品列表</h1><hr><ul><li v-for="i in products">{{ i.name }} -- ¥{{ i.price }}<button @click="cartStore.add(i)" :disabled="i.inventory <= 0">放入购物车</button></li></ul><h1>购物车</h1><hr><ul><li v-for="i in carts">姓名:{{ i.name }} ---价格:{{ i.price }} ----- 数量:{{ i.quality }} ---商品总价:{{ i.quality * i.price }}</li></ul><h1>总价格:{{ total }}</h1></div></template><script setup lang='ts'>
import { storeToRefs } from "pinia";
import { onMounted } from "vue";
import useProductStore from "./store/productorStore"
import useCartStore from "./store/cartStore"const productStore = useProductStore()
const cartStore = useCartStore()
const { products } = storeToRefs(productStore)
const { carts,total } = storeToRefs(cartStore)onMounted(()=>{productStore.loadDate()
})</script><style scoped lang='scss'></style>

三、模拟后台接口

1.新建一个json文件

{"data":[{"id":1,"name":"iphone12","price":12000,"inventory":3},{"id":2,"name":"小米10","price":3000,"inventory":10},{"id":3,"name":"华为","price":5000,"inventory":7}]
}

2、安装json-server

npm i json-server -g

3、使用

json-server ./src/data/api.json -p 9000 

http://www.lryc.cn/news/25858.html

相关文章:

  • 服务器配置 | 在Windows本地打开服务器端Tensorboard结果
  • 13 nuxt3学习(新建页面 内置组件 assets 路由)
  • Linus命令记录(持续编辑版)
  • 玩转ThreadLocal
  • 亚马逊二审来袭,跨境电商传统验证算法真的靠谱吗?
  • 微信小程序|基于小程序+云开发制作一个租房小程序
  • 2.4 群辉驱动:多网口,系统网络只能识别两个网口 解决教程
  • Android正确使用资源res文件
  • 5分钟搭建第一个k8s集群
  • 【MySQL】查询操作(基础篇)
  • 工程管理系统+spring cloud 系统管理+java 系统设置+二次开发
  • MyBatisPlus Study Notes
  • 【Vu3 测试篇】自动化测试
  • Android system实战 — Android R(11) 第三方apk权限
  • 面试总结1
  • 【Hello Linux】程序地址空间
  • 电脑崩溃蓝屏问题如何重装系统
  • 《商用密码应用与安全性评估》第一章密码基础知识1.2密码评估基本原理
  • 【编程基础之Python】7、Python基本数据类型
  • Kakfa详解(一)
  • 图解LeetCode——剑指 Offer 12. 矩阵中的路径
  • particles在vue3中的基本使用
  • 04 Android基础--RelativeLayout
  • python基础命令
  • 用 Real-ESRGAN 拯救座机画质,自制高清版动漫资源
  • 数据结构预备知识(模板)
  • SWM181按键控制双通道PWM固定占空比输出
  • pygame函数命令
  • 异步循环
  • Vue表单提交与数据存储