Vue全局共享數(shù)據(jù)之globalData,vuex,本地存儲(chǔ)的使用
寫在最前面,把vue能用到的存儲(chǔ)方法都整理拿出來(lái),方便閱讀以及工作用。
Vue全局共享數(shù)據(jù)之globalData,vuex,本地存儲(chǔ)使用方法
一、globalData
小程序有g(shù)lobalData,這是一種簡(jiǎn)單的全局變量機(jī)制。這套機(jī)制在uni-app里也可以使用,并且全端通用。
js中操作globalData的方式如下:
getApp().globalData.text = 'test'
在應(yīng)用onLaunch時(shí),getApp對(duì)象還未獲取,暫時(shí)可以使用this.globalData獲取globalData。
如果需要把globalData的數(shù)據(jù)綁定到頁(yè)面上,可在頁(yè)面的onShow頁(yè)面生命周期里進(jìn)行變量重賦值。
nvue的weex編譯模式中使用globalData的話,由于weex生命周期不支持onShow,但熟悉5+的話,可利用監(jiān)聽webview的addEventListener show事件實(shí)現(xiàn)onShow效果,或者直接使用weex生命周期中的beforeCreate。但建議開發(fā)者使用uni-app編譯模式,而不是weex編譯模式。
globalData是簡(jiǎn)單的全局變量,如果使用狀態(tài)管理,請(qǐng)使用vuex(main.js中定義)
具體可以看官網(wǎng):uni-app官網(wǎng)

在js中操作globalData的方式如下:
獲取方式:
getApp().globalData.text='test'
二、vuex存儲(chǔ)方式
1.vue2用法
2.vue3用法
//store下的index.js存儲(chǔ)vuex數(shù)據(jù)
import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
const vuexLocal = new VuexPersistence({
storage: window.localStorage,
});
export default new Vuex.Store({
state: {
count:20
},
plugins: [vuexLocal.plugin],
});
//vue3
state: {
passwordState:false,//登錄狀態(tài)
},
mutations:{
// 設(shè)置修改登錄狀態(tài)的方法
setPasswordState(state,value){
state.passwordState = value;
},
}
//VUE2中VueX用法
import {mapState } from "vuex";
export default {
computed: {
...mapState({count:'count'}),//方法2
},
computed: mapState({
count: 'count', //方法3
count: (Store) => Store.count, // 方法4
count: function (Store) { // 方法5
return '123:' + Store.count
},
}),
methods:{
submit2(){
console.log(this.$store.state.count,"===");//方法1
console.log(this.count,"count")
}
},
}
//vue3中VueX用法
const storeState=mapState(['count','name'])
const state={}
Object.keys(storeState).forEach(Key=>{
const fn=storeState[Key].bind({$store:store})
state[Key]=computed(fn)
})
//返回...state就行了
//(2)使用computed一個(gè)一個(gè)解析
import {useStore} from 'vuex'
import {computed} from 'vue'
const store=useStore()
const state=computed(()=>store.state.name)
======================================================
import { onMounted } from 'vue'
import { useStore } from 'vuex'
export default {
setup(){
//把useStore賦值
const $store = useStore();
onMounted(()=>{
//拿到vuex的值
console.log($store.state.passwordState) // false
//改變vuex的值
$store.commit('setPasswordState',true) //調(diào)用vuex的方法
//再次打印
console.log($store.state.passwordState) // true
})
return{
}
}
}三、本地存儲(chǔ)
localStorage:可長(zhǎng)期存儲(chǔ)數(shù)據(jù),除非用戶清楚localStorage信息,否則數(shù)據(jù)會(huì)一直存在。同一中瀏覽器之間,不同頁(yè)面,數(shù)據(jù)可以共享。sessionStorage:短期存儲(chǔ)數(shù)據(jù),用戶關(guān)閉標(biāo)簽頁(yè)后或直接關(guān)閉瀏覽器后數(shù)據(jù)會(huì)清空。同一瀏覽器不同頁(yè)面之間,數(shù)據(jù)不可共享
存儲(chǔ)用法:
// 將this.pickerItem的數(shù)據(jù)存儲(chǔ)入insuranceCode,需提前轉(zhuǎn)化成string類型
pickerItem:{
id: that.item.id,
limit: 100,
page: 1,
}
//長(zhǎng)期存儲(chǔ)
localStorage.setItem("insuranceCode", JSON.stringify(this.pickerItem));
//短期存儲(chǔ)
sessionStorage.setItem("insuranceCode", JSON.stringify(this.pickerItem));讀取用法,如何獲取到:
//長(zhǎng)期存儲(chǔ)
localStorage.getItem("insuranceCode")
//短期存儲(chǔ)
sessionStorage.getItem("insuranceCode")清除用法:
// 清除insuranceCode
localStorage.removeItem("insuranceCode");
sessionStorage.removeItem("insuranceCode");
// 清除所有
localStorage.clear();
sessionStorage.clear();最后補(bǔ)充個(gè)uniapp的數(shù)據(jù)緩存。
uniapp的數(shù)據(jù)緩存

這里就整個(gè)經(jīng)常用的,其他的可以看下面的圖片。 懶....
//uni.setStorageSync(KEY,DATA) 存儲(chǔ)
try {
uni.setStorageSync('storage_key', 'hello');
} catch (e) {
// error
}
//uni.getStorageSync(KEY)
//從本地緩存中同步獲取指定 key 對(duì)應(yīng)的內(nèi)容。
try {
const value = uni.getStorageSync('storage_key');
if (value) {
console.log(value);
}
} catch (e) {
// error
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
VueX?mapGetters獲取Modules中的Getters方式
這篇文章主要介紹了VueX?mapGetters獲取Modules中的Getters方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
Vue3中泛型使用unknown優(yōu)于any問(wèn)題
在Vue3中,建議優(yōu)先使用`unknown`而不是`any`,因?yàn)閌unknown`提供了更好的類型安全保證,在組件Props、組合式函數(shù)、事件處理和類型守衛(wèi)中使用`unknown`,可以有效避免運(yùn)行時(shí)錯(cuò)誤,保持TypeScript的類型安全優(yōu)勢(shì)2025-10-10
Vue實(shí)現(xiàn)具備掃描和查看數(shù)據(jù)的二維碼
在我們生活中,二維碼的應(yīng)用越來(lái)越廣泛,特別是在移動(dòng)互聯(lián)網(wǎng)的時(shí)代,二維碼成為了快速傳達(dá)信息的一種利器,本文我們就來(lái)看看如何在Vue框架下,實(shí)現(xiàn)一個(gè)具備掃描和查看數(shù)據(jù)的二維碼吧2023-05-05
一個(gè)Vue視頻媒體多段裁剪組件的實(shí)現(xiàn)示例
這篇文章主要介紹了一個(gè)Vue媒體多段裁剪組件的實(shí)現(xiàn)示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
vue?element?ui?Select選擇器如何設(shè)置默認(rèn)狀態(tài)
這篇文章主要介紹了vue?element?ui?Select選擇器如何設(shè)置默認(rèn)狀態(tài)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,2023-10-10
vue動(dòng)態(tài)禁用控件綁定disable的例子
今天小編就為大家分享一篇vue動(dòng)態(tài)禁用控件綁定disable的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
Vue項(xiàng)目中input框focus時(shí)不調(diào)出鍵盤問(wèn)題的解決
這篇文章主要介紹了Vue項(xiàng)目中input框focus時(shí)不調(diào)出鍵盤問(wèn)題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue3 reactive函數(shù)用法實(shí)戰(zhàn)教程
reactive是Vue3中提供實(shí)現(xiàn)響應(yīng)式數(shù)據(jù)的方法,reactive的用法與ref的用法相似,也是將數(shù)據(jù)變成響應(yīng)式數(shù)據(jù),當(dāng)數(shù)據(jù)發(fā)生變化時(shí)UI也會(huì)自動(dòng)更新,這篇文章主要介紹了vue3 reactive函數(shù)用法,需要的朋友可以參考下2022-11-11
詳解從vue-loader源碼分析CSS Scoped的實(shí)現(xiàn)
這篇文章主要介紹了詳解從vue-loader源碼分析CSS Scoped的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

