Vue全局共享數(shù)據(jù)之globalData,vuex,本地存儲(chǔ)的使用
寫(xiě)在最前面,把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)聽(tīng)webview的addEventListener show事件實(shí)現(xiàn)onShow效果,或者直接使用weex生命周期中的beforeCreate。但建議開(kāi)發(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ù),除非用戶(hù)清楚localStorage信息,否則數(shù)據(jù)會(huì)一直存在。同一中瀏覽器之間,不同頁(yè)面,數(shù)據(jù)可以共享。sessionStorage:短期存儲(chǔ)數(shù)據(jù),用戶(hù)關(guān)閉標(biāo)簽頁(yè)后或直接關(guān)閉瀏覽器后數(shù)據(jù)會(huì)清空。同一瀏覽器不同頁(yè)面之間,數(shù)據(jù)不可共享
存儲(chǔ)用法:
// 將this.pickerItem的數(shù)據(jù)存儲(chǔ)入insuranceCode,需提前轉(zhuǎn)化成string類(lèi)型
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)文章
vue.js 實(shí)現(xiàn)輸入框動(dòng)態(tài)添加功能
這篇文章主要介紹了vue.js 實(shí)現(xiàn)輸入框動(dòng)態(tài)添加功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
vue計(jì)算屬性computed--getter和setter用法
這篇文章主要介紹了vue計(jì)算屬性computed--getter和setter用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
vue列表單項(xiàng)展開(kāi)收縮功能之this.$refs的詳解
這篇文章主要介紹了vue列表單項(xiàng)展開(kāi)收縮功能之this.$refs的詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
vue el-form一行里面放置多個(gè)el-form-item的實(shí)現(xiàn)
本文主要介紹了vue el-form一行里面放置多個(gè)el-form-item的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Vue自動(dòng)構(gòu)建發(fā)布腳本的方法示例
這篇文章主要介紹了Vue自動(dòng)構(gòu)建發(fā)布腳本的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Vue3 響應(yīng)式數(shù)據(jù) reactive使用方法
這篇文章主要介紹了Vue3 響應(yīng)式數(shù)據(jù) reactive使用方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-11-11
vue動(dòng)態(tài)修改頁(yè)面title的兩種方法
本文主要介紹了vue動(dòng)態(tài)修改頁(yè)面title的兩種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
vue-cli 腳手架基于Nightwatch的端到端測(cè)試環(huán)境的過(guò)程
這篇文章主要介紹了vue-cli 腳手架基于Nightwatch的端到端測(cè)試環(huán)境的過(guò)程,需要的朋友可以參考下2018-09-09

