Vue2 listens for changes in localstorage values

Sometimes, we need to make corresponding operations according to the change of the value of the local cache in localstorage, which requires us to monitor localstorage:
First, we create a new stroage.js under the libs folder under src to rewrite the setItem event , when using setItem, trigger, window.dispatchEvent dispatch event
insert image description here

const Stroage = {
   
    
    
  // 重写setItem事件,当使用setItem的时候,触发,window.dispatchEvent派发事件
  dispatchEventStroage () {
   
    
    
    const signSetItem = localStorage.setItem
    localStorage

Guess you like

Origin blog.csdn.net/qq_37635012/article/details/132596374