《Vue3学习手记5》

pinia

共享的数据交给集中状态管理

引入与使用

clike 复制代码
//main.ts
// 引入Pinia
import {createPinia} from "pinia"

const pinia=createPinia()
app.use(pinia)

案例:

clike 复制代码
<template>
  <div class="count">
    <h2>当前和为:{{ sum }}-----学习{{ subject }}</h2>
    <h2>sum放大十倍后:{{ bigSum }}</h2>
    <select v-model.number="n"> 
        <!-- 把n转成数字型 -->
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
    <button @click="add">加n</button>
    <button @click="reduce">减n</button>
  </div>
  <hr>
  <div class="top">

  </div>
</template>

<script setup name="Count">
import {ref,toRefs} from "vue"
import {useCountStore} from "@/store/Count"
import { storeToRefs } from "pinia"
const countStore=useCountStore()
// console.log("@@@",countStore)  
// ********************************************************************   
// 知识点二:
    // 当需要的数据很多时,模板中需要写的countStore就很多,可以简化。
    // const {sum,subject}=toRefs(countStore)

    // 但是这样写会将countStore中所有数据都变成响应式,最好使用storeToRefs
    
    const {sum,subject,bigSum}=storeToRefs(countStore)
    const n=ref(1)
    function add(){
// ********************************************************************
// 知识点一:
    //第一种修改方法: 
        // pinia可以直接对数据进行修改,但vuex不可以
        // countStore.sum+=n.value   //注意这里的sum没有value

    // 第二种修改方法:(碎片)
        // countStore.$patch({
        //     sum:88,  //点击加,sum变为88
        // })

    // 第三种修改方法:(适合进行复杂处理时使用),具体见Store/Count.ts
        countStore.increment(n.value)
    }
    function reduce(){
        countStore.sum-=n.value
    }
</script>

<style scoped>
    .count {
        width: 900px;
        height: 250px;
        border-radius: 10px;
        box-shadow: 0 0 10px;
        background-color: skyblue;
        margin:20px;
        padding:10px;
    }
    button {
        margin-left:5px;
        margin-right:5px;
        width: 60px;
        height: 30px;
    }
</style>

在src下新建store文件夹:

clike 复制代码
//store/Count.ts:
import {defineStore} from "pinia"
export const useCountStore=defineStore("count",{  //第一个参数最好和文件名保持一致
    actions:{
        increment(value){  //接受传递过来的n.value
            if(this.sum<10){
                // console.log(this) //读取sum,不能写state.sum,作用域不同,不能拿到state
                this.sum+=value
            }
        }
        // 当sum加到10不再加
        
    },
    state(){
        return {
            sum:6,
            subject:"Vue3",
        }
    }
     // 知识点三:
    getters:{
        bigSum(state){
            return state.sum*10   //也可以写this.sum*=10
        }
    }
})  

案例二($subscribe)

store/Talk.ts:

clike 复制代码
// 知识点四:$subscribe
    state(){
        return {
         talkList:JSON.parse(localStorage.getItem("talkList") as string) || []
        }
    }

组件中:

clike 复制代码
talkStore.$subscribe((mutate,state)=>{
    localStorage.setItem("talkList",JSON.stringify(state.talkList))
    // 将state.talkList转化为字符串存入talkList
})

store组合式写法

clike 复制代码
//  知识点五:  store组合式写法
import {reactive} from "vue"
export const useTalkStore=defineStore("LoveTalk",()=>{
    // 数据
    const talkList=reactive(JSON.parse(localStorage.getItem("talkList") as string) || [])
    // actions部分:(addtalk函数相当于actions)
    async function addtalk(){
            let result=await axios.get("https://api.uomg.com/api/rand.qinghua?format=json")
            // console.log(result.data)
            const obj={id:nanoid(),title:result.data.content}
            // console.log(obj)
                talkList.unshift(obj)
        }
        return {talkList,addtalk}
    }
)
相关推荐
脑袋大大的11 分钟前
判断当前是否为钉钉环境
开发语言·前端·javascript·钉钉·企业应用开发
军军君0120 分钟前
基于Springboot+UniApp+Ai实现模拟面试小工具二:后端项目搭建
前端·javascript·spring boot·spring·微信小程序·前端框架·集成学习
Wy. Lsy38 分钟前
Kotlin基础学习记录
开发语言·学习·kotlin
The_Killer.42 分钟前
格密码--数学基础--06对偶空间与对偶格
学习·线性代数·密码学
quweiie1 小时前
tp8.0\jwt接口安全验证
前端·安全·jwt·thinkphp
xiaoyan20151 小时前
最新Flutter3.32+Dart3仿微信App聊天实例
前端·flutter·dart
汪敏wangmin2 小时前
Fiddler-抓包后直接生成Loadrunner脚本或者Jmeter脚本
前端·jmeter·fiddler
彤银浦2 小时前
Web学习笔记3
前端·笔记·学习·html5
之歆2 小时前
Python-魔术方法-创建、初始化与销毁-hash-bool-可视化-运算符重载-容器和大小-可调用对象-上下文管理-反射-描述器-二分-学习笔记
笔记·python·学习
江城开朗的豌豆2 小时前
退出登录后头像还在?这个缓存问题坑过多少前端!
前端·javascript·vue.js