vue3項目使用pinia狀態(tài)管理器的使用
1、首先安裝pinia
yarn add pinia # 或使用npm npm install pinia
2、在項目的src目錄下新建store文件夾,然后store目錄下新建index.js / index.ts :
我這里是index,js
import { createPinia } from "pinia"
// 創(chuàng)建 Pinia 實例
const pinia = createPinia()
// 導出 Pinia 實例以便將其與應用程序實例關聯(lián)
export default pinia3、 接著在項目入口文件main.js 或 main.ts 引入并使用:
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import router from './router/router'
import store from './store/index'
createApp(App)
.use(router)
.use(store)
.mount('#app')
4、然后使用defineStore來定義狀態(tài)存儲模塊,一般使用useXXXXXStore 來約定俗成的命名規(guī)范, 我這里是user.js:
import { defineStore } from "pinia"
export const useUserStore = defineStore({
//id 是為了更好地區(qū)分模塊
id: 'user',
state: () => ({
name: 'Tony',
age: 18,
count: 0
}),
getters: {
doubleCount: (state) => state.count * 2,
},
actions: {
// 定義操作或異步請求
increment() {
// 這里訪問state的數(shù)據(jù)不再是state.XXX,而是通過this
this.count++
}
}
})5、在組件內(nèi)使用store:
<template>
<div>
<h3>我是測試pinia狀態(tài)存儲的組件,我有一個子組件</h3>
<div>userStore里的state數(shù)據(jù):</div>
<span>姓名: {{ name }}</span> <span>年齡: {{ age }}</span>
<div><button @click="handleIncre">修改count:</button>count: {{ count }}</div>
<!-- 直接調(diào)用getters的方法 -->
<div> Double count is: {{ doubleCount }}</div>
</div>
</template>js:
<script setup>
import { ref, reactive } from 'vue'
import TestChild1 from './component/TestChild1.vue'
import { useUserStore } from '../../store/user';
import { storeToRefs } from 'pinia'
const userStore = useUserStore()
// 通過storeToRefs包裹,解構出來的屬性/方法才有響應式
const { name, age, count, doubleCount} = storeToRefs(userStore)
// console.log('userStore:', userStore.name, userStore.age, userStore.count)
// console.log(name.value, age.value, count.value);
// 調(diào)用store的actions的increment方法
const handleIncre = () => {
userStore.increment()
}
</script>解構store的變量或方法時,如果沒有通過storeToRefs包裹,就失去了響應式:

具有響應式:


6、在store中定義異步獲取用戶信息方法:
6.1首先新建一個api文件夾定義模擬異步登錄獲取用戶登錄信息接口方法:
~~src/api/index
// 模擬異步登錄獲取用戶信息
const loginUser = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({
name: 'Tony',
age: 18
})
}, 2000)
})
}
export default {
loginUser
}6.2 在store,user.js中:
引入api文件,在actions中定義getUserInfo方法,異步查詢時,通常都是async和await一起搭配使用的。
import { defineStore } from "pinia"
import API from '../api/index'
export const useUserStore = defineStore({
//id 是為了更好地區(qū)分模塊
id: 'user',
state: () => ({
name: 'Tony',
age: 18,
count: 0,
userInfo: {}
}),
getters: {
doubleCount: (state) => state.count * 2,
},
actions: {
// 定義操作或異步請求
increment() {
// 這里訪問state的數(shù)據(jù)不再是state.XXX,而是通過this
this.count++
},
// 在actions中異步獲取loginUser的數(shù)據(jù)
async getUserInfo() {
this.userInfo = await API.loginUser()
console.log('user-info', this.userInfo);
}
}
})6.3 在vue組件中使用:
<!-- 點擊---異步登錄獲取userInfo -->
<button @click="getUser">異步登錄獲取userInfo</button>
<div>userInfo: {{ userInfo }}</div>// 調(diào)用store的actions的getUserInfo方法異步獲取用戶登錄信息
const getUser = () => {
userStore.getUserInfo()
}
以上就是pinia的vue3使用,后面更新持續(xù)化存儲。更多相關vue3 pinia狀態(tài)管理器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Vue3之狀態(tài)管理器(Pinia)詳解及使用方式
- Vue3中的pinia使用方法總結(建議收藏版)
- Vue3狀態(tài)管理之Pinia的入門使用教程
- vue3項目引入pinia報錯的簡單解決
- vue3 pinia踩坑及解決方案詳解
- Vue3中pinia的使用與持久化處理詳解
- Vue3組件不發(fā)生變化如何監(jiān)聽pinia中數(shù)據(jù)變化
- vue3中vuex與pinia的踩坑筆記記錄
- vue3搭配pinia的踩坑實戰(zhàn)記錄
- vue3?pinia實現(xiàn)持久化詳解
- vue3中使用pinia(大菠蘿)狀態(tài)管理倉庫的項目實踐
- Vue3 狀態(tài)管理 Pinia 入門指南
相關文章
詳解如何實現(xiàn)Element樹形控件Tree在懶加載模式下的動態(tài)更新
這篇文章主要介紹了詳解如何實現(xiàn)Element樹形控件Tree在懶加載模式下的動態(tài)更新,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04
vue中三種插槽(默認插槽/具名插槽/作用域插槽)的區(qū)別詳解
默認插槽,具名插槽,作用域插槽是vue中常用的三個插槽,這篇文章主要為大家介紹了這三種插槽的使用與區(qū)別,感興趣的小伙伴可以了解一下2023-08-08
vue項目keepAlive配合vuex動態(tài)設置路由緩存方式
vue項目keepAlive配合vuex動態(tài)設置路由緩存方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04

