vue3項(xiàng)目使用pinia狀態(tài)管理器的使用
1、首先安裝pinia
yarn add pinia # 或使用npm npm install pinia
2、在項(xiàng)目的src目錄下新建store文件夾,然后store目錄下新建index.js / index.ts :
我這里是index,js
import { createPinia } from "pinia" // 創(chuàng)建 Pinia 實(shí)例 const pinia = createPinia() // 導(dǎo)出 Pinia 實(shí)例以便將其與應(yīng)用程序?qū)嵗P(guān)聯(lián) export default pinia
3、 接著在項(xiàng)目入口文件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來(lái)定義狀態(tài)存儲(chǔ)模塊,一般使用useXXXXXStore 來(lái)約定俗成的命名規(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: { // 定義操作或異步請(qǐng)求 increment() { // 這里訪問state的數(shù)據(jù)不再是state.XXX,而是通過(guò)this this.count++ } } })
5、在組件內(nèi)使用store:
<template> <div> <h3>我是測(cè)試pinia狀態(tài)存儲(chǔ)的組件,我有一個(gè)子組件</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() // 通過(guò)storeToRefs包裹,解構(gòu)出來(lái)的屬性/方法才有響應(yīng)式 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>
解構(gòu)store的變量或方法時(shí),如果沒有通過(guò)storeToRefs包裹,就失去了響應(yīng)式:
具有響應(yīng)式:
6、在store中定義異步獲取用戶信息方法:
6.1首先新建一個(gè)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方法,異步查詢時(shí),通常都是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: { // 定義操作或異步請(qǐng)求 increment() { // 這里訪問state的數(shù)據(jù)不再是state.XXX,而是通過(guò)this this.count++ }, // 在actions中異步獲取loginUser的數(shù)據(jù) async getUserInfo() { this.userInfo = await API.loginUser() console.log('user-info', this.userInfo); } } })
6.3 在vue組件中使用:
<!-- 點(diǎn)擊---異步登錄獲取userInfo --> <button @click="getUser">異步登錄獲取userInfo</button> <div>userInfo: {{ userInfo }}</div>
// 調(diào)用store的actions的getUserInfo方法異步獲取用戶登錄信息 const getUser = () => { userStore.getUserInfo() }
以上就是pinia的vue3使用,后面更新持續(xù)化存儲(chǔ)。更多相關(guān)vue3 pinia狀態(tài)管理器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue3+Vite項(xiàng)目中引入pinia和pinia-plugin-persistedstate的方法代碼
- vue3配置permission.js和router、pinia實(shí)現(xiàn)路由攔截的簡(jiǎn)單步驟
- vue3?pinia實(shí)現(xiàn)持久化詳解
- vue3中pinia的使用方法
- vue3 pinia使用及持久化注冊(cè)
- Vue狀態(tài)管理工具Pinia的安裝與使用教程
- vue3使用Pinia修改state的三種方法(直接修改,$patch,actions)
- Vue3之狀態(tài)管理器(Pinia)詳解及使用方式
- Vue中Pinia的各種詳細(xì)說(shuō)明和使用示例
相關(guān)文章
vue3使用拖拽組件draggable.next的保姆級(jí)教程
做項(xiàng)目的時(shí)候遇到了一個(gè)需求,拖拽按鈕到指定位置,添加一個(gè)輸入框,這篇文章主要給大家介紹了關(guān)于vue3使用拖拽組件draggable.next的保姆級(jí)教程,需要的朋友可以參考下2023-06-06基于 vue-skeleton-webpack-plugin 的骨架屏實(shí)戰(zhàn)
這篇文章主要介紹了基于 vue-skeleton-webpack-plugin 的骨架屏實(shí)戰(zhàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08vue+element樹組件 實(shí)現(xiàn)樹懶加載的過(guò)程詳解
這篇文章主要介紹了vue+element樹組件 實(shí)現(xiàn)樹懶加載的過(guò)程,本文通過(guò)圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10Vue中"This dependency was not found"問題的解決方法
這篇文章主要介紹了Vue中"This dependency was not found"的問題的解決方法,需要的朋友可以參考下2018-06-06vue實(shí)現(xiàn)鼠標(biāo)滑動(dòng)預(yù)覽視頻封面組件示例詳解
這篇文章主要為大家介紹了vue實(shí)現(xiàn)鼠標(biāo)滑動(dòng)預(yù)覽視頻封面組件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07