Vue3 Pinia獲取全局狀態(tài)變量的實(shí)現(xiàn)方式
使用說明
在 Pinia 中,獲取狀態(tài)變量的方式非常的簡單 : 就和使用對象一樣。
使用思路:
- 1、導(dǎo)入Store
- 2、聲明Store對象
- 3、使用對象
在邏輯代碼中使用
但是 Option Store 和 Setup Store 兩種方式定義的全局狀態(tài)變量在獲取的時(shí)候還是有簡單的區(qū)別的:
Option Store: 聲明Store對象之后,可以直接使用屬性,例如 : 【store.name】Setup Store: 聲明Store對象之后,可以獲取到定義的聲明式對象,所以使用具體屬性時(shí)需要通過 該對象,例如 : 【store.student.name】
在html模板中使用
此處非常的簡單,Store對象中有一個(gè)$state 屬性,這個(gè)屬性就是我們定義的全局狀態(tài)變量。
下面通過具體的案例體會一下。
具體案例
本案例 有一個(gè)全局狀態(tài)變量的 配置文件,分別通過 Option Store 和 Setup Store 兩種方式定義了兩個(gè)全局狀態(tài)變量;
在組件A 中 導(dǎo)入兩個(gè)全局狀態(tài)變量,并分別在控制臺 和 頁面模板中讀取展示一下;
在 App.vue 文件中 存在 <router-view> 標(biāo)簽用于組件的路由。
全局狀態(tài)變量配置文件
// 導(dǎo)入 defineStore API
import { defineStore } from 'pinia'
// 導(dǎo)入 reactive 依賴
import { reactive } from 'vue'
// 定義全局狀態(tài)方式一 : option store
export const useClassStore = defineStore('classinfo',{
state: () => ({
name:'快樂籃球二班',
studentNum:30
})
})
// 定義全局狀態(tài)方式二 : setup store
export const useStudentStore = defineStore('studentinfo',() => {
// 響應(yīng)式狀態(tài) : student 是響應(yīng)式對象
const student = reactive({
name : '小明',
age:12,
className:'快樂足球一班'
})
return { student }
})
App.vue 組件
<template>
<div class="basediv">
APP.vue 中的 msg : {{ msg }}
<br>
<br>
<!-- 組件放在這里 -->
<router-view></router-view>
</div>
</template>
<script setup lang="ts">
// 引入 provide 方法
import { ref } from 'vue'
// 聲明父組件的一個(gè)變量
const msg = ref('這是App根組件的msg變量')
</script>
<style scoped>
.basediv{
width: 600px;
height: 400px;
border: 1px solid red;
}
</style>
組件A的代碼
<template>
<div class="diva">
這是組件A
<br>
<br>
<!-- 使用 $state 來讀取全局狀態(tài)變量 -->
classStore : {{ classStore.$state }}
<br>
studentStore : {{ studentStore.$state }}
</div>
</template>
<script setup lang="ts">
// 導(dǎo)入全局狀態(tài)變量的定義
import { useClassStore,useStudentStore } from './storea'
// 獲取全局狀態(tài)變量的對象
const classStore = useClassStore()
const studentStore = useStudentStore()
// 讀取一下全局的變量
console.log('組件A 中 : ',classStore)
console.log('組件A 中 : ',studentStore)
// Option Store 的方式 : 直接可以獲取到屬性
console.log('組件A 中 classinfo 對象 : ',classStore.name+' - '+classStore.studentNum)
// Setup Store 的方式 : 仍然需要通過 定義的 student 對象,因?yàn)檫@個(gè)student 是真正的全局狀態(tài)對象
console.log('組件A 中 studentinfo 數(shù)據(jù)對象: ',studentStore.student.name+'-'+studentStore.student.age+'-'+studentStore.student.className)
console.log('------')
</script>
<style scoped>
.diva{
width: 450px;
height: 250px;
background: red;
}
</style>
運(yùn)行結(jié)果

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式
這篇文章主要介紹了vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
vue中$router.back()和$router.go()的用法
這篇文章主要介紹了vue中$router.back()和$router.go()的用法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
vue-router 實(shí)現(xiàn)導(dǎo)航守衛(wèi)(路由衛(wèi)士)的實(shí)例代碼
這篇文章主要介紹了vue-router 實(shí)現(xiàn)導(dǎo)航守衛(wèi)(路由衛(wèi)士)的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09
Ant design vue table 單擊行選中 勾選checkbox教程
這篇文章主要介紹了Ant design vue table 單擊行選中 勾選checkbox教程,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
淺談vue+webpack項(xiàng)目調(diào)試方法步驟
本篇文章主要介紹了淺談vue+webpack項(xiàng)目調(diào)試方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
淺談vue中g(shù)et請求解決傳輸數(shù)據(jù)是數(shù)組格式的問題
這篇文章主要介紹了淺談vue中g(shù)et請求解決傳輸數(shù)據(jù)是數(shù)組格式的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Vue3三款爆款實(shí)用插件教程(剛需實(shí)用)
Vue被一個(gè)健康的插件和包的生態(tài)系統(tǒng)所加強(qiáng),使開發(fā)變得可靠、快速和簡單, 由于Vue是一個(gè)國際開發(fā)者社區(qū)所選擇的框架,所以有一個(gè)不斷增長的插件和包庫,你可以在項(xiàng)目中使用,這篇文章主要介紹了Vue3三款爆款實(shí)用插件教程的相關(guān)資料,需要的朋友可以參考下2026-03-03

