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

在js中操作globalData的方式如下:
獲取方式:
getApp().globalData.text='test'
二、vuex存儲方式
1.vue2用法
2.vue3用法
//store下的index.js存儲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:{
// 設置修改登錄狀態(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一個一個解析
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) //調用vuex的方法
//再次打印
console.log($store.state.passwordState) // true
})
return{
}
}
}三、本地存儲
localStorage:可長期存儲數(shù)據(jù),除非用戶清楚localStorage信息,否則數(shù)據(jù)會一直存在。同一中瀏覽器之間,不同頁面,數(shù)據(jù)可以共享。sessionStorage:短期存儲數(shù)據(jù),用戶關閉標簽頁后或直接關閉瀏覽器后數(shù)據(jù)會清空。同一瀏覽器不同頁面之間,數(shù)據(jù)不可共享
存儲用法:
// 將this.pickerItem的數(shù)據(jù)存儲入insuranceCode,需提前轉化成string類型
pickerItem:{
id: that.item.id,
limit: 100,
page: 1,
}
//長期存儲
localStorage.setItem("insuranceCode", JSON.stringify(this.pickerItem));
//短期存儲
sessionStorage.setItem("insuranceCode", JSON.stringify(this.pickerItem));讀取用法,如何獲取到:
//長期存儲
localStorage.getItem("insuranceCode")
//短期存儲
sessionStorage.getItem("insuranceCode")清除用法:
// 清除insuranceCode
localStorage.removeItem("insuranceCode");
sessionStorage.removeItem("insuranceCode");
// 清除所有
localStorage.clear();
sessionStorage.clear();最后補充個uniapp的數(shù)據(jù)緩存。
uniapp的數(shù)據(jù)緩存

這里就整個經常用的,其他的可以看下面的圖片。 懶....
//uni.setStorageSync(KEY,DATA) 存儲
try {
uni.setStorageSync('storage_key', 'hello');
} catch (e) {
// error
}
//uni.getStorageSync(KEY)
//從本地緩存中同步獲取指定 key 對應的內容。
try {
const value = uni.getStorageSync('storage_key');
if (value) {
console.log(value);
}
} catch (e) {
// error
}
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue計算屬性computed--getter和setter用法
這篇文章主要介紹了vue計算屬性computed--getter和setter用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
vue el-form一行里面放置多個el-form-item的實現(xiàn)
本文主要介紹了vue el-form一行里面放置多個el-form-item的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08
Vue3 響應式數(shù)據(jù) reactive使用方法
這篇文章主要介紹了Vue3 響應式數(shù)據(jù) reactive使用方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-11-11
vue-cli 腳手架基于Nightwatch的端到端測試環(huán)境的過程
這篇文章主要介紹了vue-cli 腳手架基于Nightwatch的端到端測試環(huán)境的過程,需要的朋友可以參考下2018-09-09

