Vue3中使用pinia的示例代碼
1、安裝:npm install pinia
2、創(chuàng)建store文件并配置內(nèi)部的index.js文件
import { defineStore } from 'pinia' //引入pinia //這里官網(wǎng)是單獨(dú)導(dǎo)出 是可以寫成默認(rèn)導(dǎo)出的 官方的解釋為大家一起約定倉庫用use打頭的單詞 固定統(tǒng)一小倉庫的名字不易混亂 export const useCar=defineStore("test",{ state: () =>{ return ({ msg:"這是pinia的數(shù)據(jù)", name:"小獅子", age:18 }) //為了避免出錯(cuò),返回的值用()包起來 } })
3、main.js文件中配置
import { createApp } from 'vue' import App from './App.vue' const app=createApp(App) import { createPinia } from 'pinia' //引入pinia app.use(createPinia()) app.mount('#app')
4、組件使用
<template> <h1>aaa--{{store.msg}}----{{store.name}}--{{store.age}}</h1> <button @click="change1">修改store.name</button> <router-view></router-view> </template> <script setup> import {useCar} from "../store/index.js" //將之前配置的pinia文件夾中的index.js文件引入 let store=useCar() //接收 console.log(store) let change1=()=>{ store.name="小羊" //修改pinia里面的數(shù)據(jù) console.log(store.name) } </script> <style scoped> h1{ width: 400px; height:200px; background-color:deeppink; } </style>
效果圖
點(diǎn)擊按鈕,界面變化
說明成功修改了pinia里面的數(shù)據(jù),且界面刷新證明這種直接修該pinia數(shù)據(jù)的方式依然是響應(yīng)式數(shù)據(jù)。
但如果在接收pinia數(shù)據(jù)時(shí),進(jìn)行解構(gòu)則不再是響應(yīng)式數(shù)據(jù),需要使用toRefs
toRefs
4-1、 store.$reset()
將狀態(tài) 重置 到其初始值
當(dāng)我們接收到pinia數(shù)據(jù)且對(duì)其數(shù)據(jù)進(jìn)行了大量修改又想還原時(shí),調(diào)用此方法就可以將接收的pinia數(shù)據(jù)全部重置還原
注意:store.$reset() 中的store是自己設(shè)定的接收變量,重點(diǎn)是 .$reset()
<template> <h1>aaa--{{store.msg}}----{{store.name}}--{{store.age}}</h1> <button @click="change1">修改store.name</button> <button @click="reset">reset</button> <router-view></router-view> </template> <script setup> import {useCar} from "../store/car.js" let store=useCar() console.log(store) let change1=()=>{ store.name="小羊" console.log(store.name) } let reset=()=>{ //初始化pinia數(shù)據(jù) store.$reset() } </script> <style scoped> h1{ width: 400px; height:200px; background-color:deeppink; } </style>
在之前的案例中修改了pinia的name屬性值
此時(shí)我們點(diǎn)擊reset按鈕,則會(huì)重置pinia的所有數(shù)據(jù)
4-2 store.$patch
群體修改,可以將pinia的數(shù)據(jù)進(jìn)行同一修改
特點(diǎn):批量修改但狀態(tài)只刷新一次
<template> <h1>aaa--{{store.msg}}----{{store.name}}--{{store.age}}</h1> <button @click="change1">修改store.name</button> <button @click="reset">reset</button> <button @click="fn">fn</button> <router-view></router-view> </template> <script setup> import {useCar} from "../store/car.js" let store=useCar() console.log(store) let change1=()=>{ store.name="小羊" console.log(store.name) } let reset=()=>{ //重置 store.$reset() } function fn(){ //批量修改 store.$patch({ name:"小羊", age:20, }) } </script> <style scoped> h1{ width: 400px; height:200px; background-color:deeppink; } </style>
點(diǎn)擊fn按鈕后
說明批量修改成功
5.訂閱修改
//可以通過 store 的 $subscribe() 方法查看狀態(tài)及其變化,通過patch修改狀態(tài)時(shí)就會(huì)觸發(fā)一次 store.$subscribe((mutation, state) => { // 每當(dāng)它發(fā)生變化時(shí),將整個(gè)狀態(tài)持久化到本地存儲(chǔ) localStorage.setItem('test', JSON.stringify(state)) })
6.Getter
Getter 完全等同于 Store 狀態(tài)的 計(jì)算值。 它們可以用 defineStore()
中的 getters
屬性定義。 他們接收“狀態(tài)”作為第一個(gè)參數(shù)以鼓勵(lì)箭頭函數(shù)的使用:(ps:雖然鼓勵(lì)但是依然提供了非es6玩家的使用方式 內(nèi)部可以用this代表state)
//store/index.js文件 export const useStore = defineStore('main', { state: () => ({ counter: 0, }), getters: { doubleCount: (state) => state.counter * 2, }, }) //組件中直接使用: <p>Double count is {{ store.doubleCount }}</p>
7、Actions
在pinia中沒有提供mutaion 因?yàn)锳ctions就夠了(它可以異步同步的統(tǒng)一修改狀態(tài))
之所以提供這個(gè)功能 就是為了項(xiàng)目中的公共修改狀態(tài)的業(yè)務(wù)統(tǒng)一
export const useStore = defineStore('main', { state: () => ({ counter: 0, }), actions: { increment() { this.counter++//1.有this }, randomizeCounter(state) {//2.有參數(shù) 想用哪個(gè)用哪個(gè) state.counter = Math.round(100 * Math.random()) }, randomizeCounter(state) { //異步函數(shù) axios("/test").then(res=>{ state.counter = res.data.length }) } }, }) //組件中的使用: setup() { const store = useStore() store.increment() store.randomizeCounter() }
到此這篇關(guān)于Vue3中使用pinia的文章就介紹到這了,更多相關(guān)Vue3中使用pinia內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3前端生成隨機(jī)id(生成?UUID)實(shí)際運(yùn)用
前端在做增刪改查時(shí)通常會(huì)使??個(gè)唯?數(shù)值做為數(shù)據(jù)的key值,下面這篇文章主要給大家介紹了關(guān)于Vue3前端生成隨機(jī)id(生成?UUID)的相關(guān)資料,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2024-04-04vue 路由meta 設(shè)置導(dǎo)航隱藏與顯示功能的示例代碼
這篇文章主要介紹了vue 路由meta 設(shè)置導(dǎo)航隱藏與顯示功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Vue開發(fā)過程中遇到的疑惑知識(shí)點(diǎn)總結(jié)
vue是法語中視圖的意思,Vue.js是一個(gè)輕巧、高性能、可組件化的MVVM庫,同時(shí)擁有非常容易上手的API。下面這篇文章主要給大家總結(jié)了Vue在開發(fā)過程中遇到的疑惑知識(shí)點(diǎn),有需要的朋友可以參考借鑒,下面來一起看看吧。2017-01-01如何使用Vue3+Vite+TS快速搭建一套實(shí)用的腳手架
Vite是一個(gè)面向現(xiàn)代瀏覽器的一個(gè)更輕、更快的?Web?應(yīng)用開發(fā)工具,下面這篇文章主要給大家介紹了關(guān)于如何使用Vue3+Vite+TS快速搭建一套實(shí)用腳手架的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10vue子路由跳轉(zhuǎn)實(shí)現(xiàn)tab選項(xiàng)卡效果
這篇文章主要為大家詳細(xì)介紹了vue子路由跳轉(zhuǎn)實(shí)現(xiàn)tab選項(xiàng)卡效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03element-plus一個(gè)vue3.xUI框架(element-ui的3.x 版初體驗(yàn))
這篇文章主要介紹了element-plus一個(gè)vue3.xUI框架(element-ui的3.x 版初體驗(yàn)),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12