Vue監(jiān)聽localstorage變化的方法詳解
一. 說明
在日常開發(fā)中,我們經(jīng)常使用localStorage來存儲一些變量。這些變量會存儲在瀏覽中。對于localStorage來說,即使關(guān)閉瀏覽器,這些變量依然存儲著,方便我們開發(fā)的時候在別的地方使用。
二. localStorage的使用
存儲值:window.localStorage.setItem(‘鍵名’, 值)
讀取值:window.localStorage.getItem('‘鍵名’)
三. 監(jiān)聽localStorage值的變化
- 1.在utils中新建一個文件watchLocalStorage.ts
export default function dispatchEventStroage() {
const signSetItem = localStorage.setItem
localStorage.setItem = function (key, val) {
let setEvent = new Event('setItemEvent')
setEvent.key = key
setEvent.newValue = val
window.dispatchEvent(setEvent)
// eslint-disable-next-line prefer-rest-params
signSetItem.apply(this, arguments)
}
}- 2. 在main.js中引入并掛載
import dispatchEventStroage from './utils/watchLocalStorage' Vue.use(dispatchEventStroage);
- 3.在需要監(jiān)聽的地方使用如下代碼
mounted () {
// 監(jiān)聽localhostStorage的數(shù)據(jù)變化,結(jié)合utils/tool.js配合使用
const that=this
window.addEventListener('setItemEvent', function (e) {
if (e.key === 'myKey') { // myKey是需要監(jiān)聽的鍵名
that.mykey = e.newValue;
console.log(JSON.parse(e.newValue)) // 這里的newValue就是localStorage中,鍵名為myKey對應(yīng)的變化后的值。
console.log('本地存儲值發(fā)生變化:', e.newValue)
}
})
},特別注意:
我剛開始做的時候,考慮不周,沒有寫const that=this 這一句,我用的如下代碼,一直報錯,賦值不了,如下代碼是錯誤的,
為什么要用const that=this這一個呢?
那是因為在JavaScript中,this代表的是當(dāng)前對象,他是會隨程序運(yùn)行不停改變的,如果用this的話,this是addEventListener監(jiān)聽中當(dāng)前的對象,所以賦值的時候不能賦值到外面去。const that = this 其實就是在this改變之前先復(fù)制一份給that,那么在程序后面的運(yùn)行中就可以用that來賦值該函數(shù)以外的對象了,比如that.order
四.另外提供一種方法 vuex
利用 const that=this,可以將值存進(jìn)store中,
this.od2Visible = true;
this.title = '艦船航線規(guī)劃';
this.$store.commit("tools/setzhk", 'od5');
var tempList = [];
const that = this;
var handler = new Cesium.ScreenSpaceEventHandler(window.viewer.scene.canvas); // 創(chuàng)建鼠標(biāo)事件handler
handler.setInputAction(function(click) { // 監(jiān)聽鼠標(biāo)左鍵點(diǎn)擊事件
// 獲取點(diǎn)擊位置的地球坐標(biāo)
var cartesian = window.viewer.camera.pickEllipsoid(click.position, window.viewer.scene.globe.ellipsoid);
// 轉(zhuǎn)換為笛卡爾坐標(biāo)系
let cartographic1 = Cesium.Cartographic.fromCartesian(cartesian);
// 轉(zhuǎn)換為經(jīng)緯度
var latitude = Cesium.Math.toDegrees(cartographic1.latitude);
var longitude = Cesium.Math.toDegrees(cartographic1.longitude);
tempList.push(longitude,latitude)
if (cartesian) {
var entity = window.viewer.entities.add({ // 在該位置添加點(diǎn)
position: cartesian,
point: {
pixelSize: 10,
color: Cesium.Color.YELLOW
}
});
}
localStorage.setItem('lonlan',tempList)
that.$store.commit("tools/setlonlat", tempList);
console.log(1001,that)
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);然后再使用 用 計算屬性 computed 和 watch 監(jiān)聽實現(xiàn)
computed: {
lonlat(){
return this.$store.state.tools.lonlat
}
},
watch: {
lonlat(newVal,oldVal){
console.log(1002,newVal,oldVal)
}
},到此這篇關(guān)于Vue監(jiān)聽localstorage變化的方法詳解的文章就介紹到這了,更多相關(guān)Vue監(jiān)聽localstorage變化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue 實現(xiàn)復(fù)制功能,不需要任何結(jié)構(gòu)內(nèi)容直接復(fù)制方式
今天小編就為大家分享一篇Vue 實現(xiàn)復(fù)制功能,不需要任何結(jié)構(gòu)內(nèi)容直接復(fù)制方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
使用 vue 實例更好的監(jiān)聽事件及vue實例的方法
這篇文章主要介紹了使用 vue 實例更好的監(jiān)聽事件及vue實例的方法,介紹了一種新增 vue 實例的方法,單獨(dú)監(jiān)聽事件,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04
Vue默認(rèn)插槽,具名插槽,作用域插槽定義及使用方法
這篇文章主要介紹了Vue默認(rèn)插槽,具名插槽,作用域插槽定義及使用方法,插槽的作用是在子組件中某個位置插入父組件的自定義html結(jié)構(gòu)和data數(shù)據(jù),下面詳細(xì)內(nèi)容需要的小伙伴可以參考一下2022-03-03
uniapp Vue3中如何解決web/H5網(wǎng)頁瀏覽器跨域的問題
存在跨域問題的原因是因為瀏覽器的同源策略,也就是說前端無法直接發(fā)起跨域請求,同源策略是一個基礎(chǔ)的安全策略,但是這也會給uniapp/Vue開發(fā)者在部署時帶來一定的麻煩,這篇文章主要介紹了在uniapp Vue3版本中如何解決web/H5網(wǎng)頁瀏覽器跨域的問題,需要的朋友可以參考下2024-06-06

