Is there any way to 'watch' for localstorage in Vuejs?

前端 未结 6 1864
伪装坚强ぢ
伪装坚强ぢ 2020-12-05 23:20

I\'m attempting to watch for localstorage:

Template:

token - {{token}}

Script:

computed: {
  t         


        
6条回答
  •  生来不讨喜
    2020-12-05 23:57

    LocalStorage or sessionStorage are not reactive. Thus you can't put a watcher on them. A solution would be to store value from a store state if you are using Vuex for example. Ex:

    SET_VALUE:(state,payload)=> {
        state.value = payload
        localStorage.setItem('name',state.value)
        or
        sessionStorage.setItem('name',state.value)
      }
    

提交回复
热议问题