Vue3中引入Pinia存儲庫使用示例詳解
1.用自己最喜歡的方式安裝
yarn add pinia # 或者使用 npm npm install pinia
2.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') 3.創(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",
name:"小小",
age:18
}) //為了避免出錯,返回的值用()包起來
}
})4.組件使用方法
<template>
<h1>{{store.msg}}{{store.name}}{{store.age}}</h1>
<button @click="modify">修改store.name</button>
</template>
<script>
import { defineComponent, ref } from 'vue';
import {
reactive,
toRefs,
onMounted,
onActivated
} from "vue";
import {useCar} from "../store/index.js" //將之前配置的pinia文件夾中的index.js文件引入
export default defineComponent({
let store=useCar() //接收
setup() {
const state = reactive({
testMsg: "原始值",
});
onMounted(async () => {});
onActivated(() => {})
const methods = {
modify(){
store.name = state.testMsg //修改pinia里面的數(shù)據(jù)
console.log(store.name)
}
};
return {
...toRefs(state),
...methods,
};
}
})
</script>
5.重置store.$reset()
<template>
<h1>{{store.msg}}{{store.name}}{{store.age}}</h1>
<button @click="reset">重置store.name</button>
</template>
<script>
import { defineComponent, ref } from 'vue';
import {
reactive,
toRefs,
onMounted,
onActivated
} from "vue";
import {useCar} from "../store/index.js" //將之前配置的pinia文件夾中的index.js文件引入
export default defineComponent({
let store=useCar() //接收
setup() {
const state = reactive({
testMsg: "原始值",
});
onMounted(async () => {});
onActivated(() => {})
const methods = {
reset(){
store.$reset() //重置pinia里面的數(shù)據(jù)
console.log(store.name)
}
};
return {
...toRefs(state),
...methods,
};
}
})
</script>
6.群體修改store.$patch,可以將pinia的數(shù)據(jù)進(jìn)行同一修改
特點(diǎn):批量修改但狀態(tài)只刷新一次
<template>
<h1>{{store.msg}}{{store.name}}{{store.age}}</h1>
<button @click="modify">修改store.name</button>
<button @click="reset">重置store.name</button>
<button @click="allModify">群體修改store.name</button>
</template>
<script>
import { defineComponent, ref } from 'vue';
import {
reactive,
toRefs,
onMounted,
onActivated
} from "vue";
import {useCar} from "../store/index.js" //將之前配置的pinia文件夾中的index.js文件引入
export default defineComponent({
let store=useCar() //接收
setup() {
const state = reactive({
testMsg: "原始值",
});
onMounted(async () => {});
onActivated(() => {})
const methods = {
modify(){
store.name = state.testMsg //修改pinia里面的數(shù)據(jù)
console.log(store.name)
},
reset(){
store.$reset() //重置pinia里面的數(shù)據(jù)
console.log(store.name)
},
allModify(){
store.$patch({
name:"花花",
age:20,
})
}
};
return {
...toRefs(state),
...methods,
};
}
})
</script>7.訂閱修改
//可以通過 store 的 $subscribe() 方法查看狀態(tài)及其變化,通過patch修改狀態(tài)時就會觸發(fā)一次
store.$subscribe((mutation, state) => {
// 每當(dāng)它發(fā)生變化時,將整個狀態(tài)持久化到本地存儲
localStorage.setItem('hello', JSON.stringify(state))
})8.Getter
Getter 完全等同于 Store 狀態(tài)的 計算值。 它們可以用 defineStore() 中的 getters 屬性定義。 他們接收“狀態(tài)”作為第一個參數(shù)以鼓勵箭頭函數(shù)的使用:(ps:雖然鼓勵但是依然提供了非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>9.Actions
在pinia中沒有提供mutaion 因?yàn)锳ctions就夠了(它可以異步同步的統(tǒng)一修改狀態(tài))之所以提供這個功能 就是為了項(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ù) 想用哪個用哪個
state.counter = Math.round(100 * Math.random())
},
randomizeCounter(state) {
//異步函數(shù).接口成功賦值
ajax.hplBaseApi("app/productSelection/categoryRows", {}, "post").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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue如何處理Axios多次請求數(shù)據(jù)顯示問題
這篇文章主要介紹了Vue如何處理Axios多次請求數(shù)據(jù)顯示問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
Vue+Jwt+SpringBoot+Ldap完成登錄認(rèn)證的示例代碼
本篇文章主要介紹了Vue+Jwt+SpringBoot+Ldap完成登錄認(rèn)證的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
解決vue報錯:Do?not?mutate?vuex?store?state?outside?mutati
這篇文章主要介紹了解決vue報錯:Do?not?mutate?vuex?store?state?outside?mutation?handlers問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
Vue利用高德地圖API實(shí)現(xiàn)實(shí)時天氣
這篇文章主要為大家詳細(xì)介紹了Vue如何利用高德地圖API實(shí)現(xiàn)實(shí)時天氣,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12

