Vue監(jiān)聽(tīng)localstorage變化的方法詳解
一. 說(shuō)明
在日常開(kāi)發(fā)中,我們經(jīng)常使用localStorage來(lái)存儲(chǔ)一些變量。這些變量會(huì)存儲(chǔ)在瀏覽中。對(duì)于localStorage來(lái)說(shuō),即使關(guān)閉瀏覽器,這些變量依然存儲(chǔ)著,方便我們開(kāi)發(fā)的時(shí)候在別的地方使用。
二. localStorage的使用
存儲(chǔ)值:window.localStorage.setItem(‘鍵名’, 值)
讀取值:window.localStorage.getItem('‘鍵名’)
三. 監(jiān)聽(tīng)localStorage值的變化
- 1.在utils中新建一個(gè)文件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)聽(tīng)的地方使用如下代碼
mounted () {
// 監(jiān)聽(tīng)localhostStorage的數(shù)據(jù)變化,結(jié)合utils/tool.js配合使用
const that=this
window.addEventListener('setItemEvent', function (e) {
if (e.key === 'myKey') { // myKey是需要監(jiān)聽(tīng)的鍵名
that.mykey = e.newValue;
console.log(JSON.parse(e.newValue)) // 這里的newValue就是localStorage中,鍵名為myKey對(duì)應(yīng)的變化后的值。
console.log('本地存儲(chǔ)值發(fā)生變化:', e.newValue)
}
})
},特別注意:
我剛開(kāi)始做的時(shí)候,考慮不周,沒(méi)有寫(xiě)const that=this 這一句,我用的如下代碼,一直報(bào)錯(cuò),賦值不了,如下代碼是錯(cuò)誤的,
為什么要用const that=this這一個(gè)呢?
那是因?yàn)樵贘avaScript中,this代表的是當(dāng)前對(duì)象,他是會(huì)隨程序運(yùn)行不停改變的,如果用this的話,this是addEventListener監(jiān)聽(tīng)中當(dāng)前的對(duì)象,所以賦值的時(shí)候不能賦值到外面去。const that = this 其實(shí)就是在this改變之前先復(fù)制一份給that,那么在程序后面的運(yùn)行中就可以用that來(lái)賦值該函數(shù)以外的對(duì)象了,比如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)聽(tīng)鼠標(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);然后再使用 用 計(jì)算屬性 computed 和 watch 監(jiān)聽(tīng)實(shí)現(xiàn)
computed: {
lonlat(){
return this.$store.state.tools.lonlat
}
},
watch: {
lonlat(newVal,oldVal){
console.log(1002,newVal,oldVal)
}
},到此這篇關(guān)于Vue監(jiān)聽(tīng)localstorage變化的方法詳解的文章就介紹到這了,更多相關(guān)Vue監(jiān)聽(tīng)localstorage變化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue 實(shí)現(xiàn)復(fù)制功能,不需要任何結(jié)構(gòu)內(nèi)容直接復(fù)制方式
今天小編就為大家分享一篇Vue 實(shí)現(xiàn)復(fù)制功能,不需要任何結(jié)構(gòu)內(nèi)容直接復(fù)制方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
23個(gè)不可錯(cuò)過(guò)的Vue3實(shí)用Hook
@vueuse/core提供了豐富的功能性鉤子,使得處理常見(jiàn)的開(kāi)發(fā)任務(wù)變得更加簡(jiǎn)潔和高效,本文將詳細(xì)介紹如何在Vue3項(xiàng)目中使用?@vueuse/core及其一些極具價(jià)值的功能,需要的可以參考下2024-11-11
使用 vue 實(shí)例更好的監(jiān)聽(tīng)事件及vue實(shí)例的方法
這篇文章主要介紹了使用 vue 實(shí)例更好的監(jiān)聽(tīng)事件及vue實(shí)例的方法,介紹了一種新增 vue 實(shí)例的方法,單獨(dú)監(jiān)聽(tīng)事件,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
Vue默認(rèn)插槽,具名插槽,作用域插槽定義及使用方法
這篇文章主要介紹了Vue默認(rèn)插槽,具名插槽,作用域插槽定義及使用方法,插槽的作用是在子組件中某個(gè)位置插入父組件的自定義html結(jié)構(gòu)和data數(shù)據(jù),下面詳細(xì)內(nèi)容需要的小伙伴可以參考一下2022-03-03
uniapp Vue3中如何解決web/H5網(wǎng)頁(yè)瀏覽器跨域的問(wèn)題
存在跨域問(wèn)題的原因是因?yàn)闉g覽器的同源策略,也就是說(shuō)前端無(wú)法直接發(fā)起跨域請(qǐng)求,同源策略是一個(gè)基礎(chǔ)的安全策略,但是這也會(huì)給uniapp/Vue開(kāi)發(fā)者在部署時(shí)帶來(lái)一定的麻煩,這篇文章主要介紹了在uniapp Vue3版本中如何解決web/H5網(wǎng)頁(yè)瀏覽器跨域的問(wèn)題,需要的朋友可以參考下2024-06-06

