[英]How can I store state for every item in react localStorage?
[英]How can I read from localStorage and mutate state in Vue store
我有我在localStorage
設置的屬性, enableLog = false
。 如果我在控制台localStorage.enableLog = true
寫入,如何在我的 vuex 存儲中讀取該值並將我的狀態從false
翻轉為true
並觸發console.log
?
我想要做的是添加臨時控制台日志記錄。 例如,我們有一個應用程序會將數據發送到另一個應用程序,該應用程序在彈出窗口中啟動。 在彈出應用程序中,如果另一個開發人員將enableLog
從false
更改為true
,他/她將能夠看到通過console.logs
函數發送到彈出應用程序的有效負載中的初始數據。 我的應用程序如何讀取localStorage
屬性,然后如何從中更新狀態,最后重新渲染以觸發調用該console.log
的函數的位置在哪里?
index.js
,狀態:
isLoggingData: false
index.js
,動作:
async init ({ state, commit, dispatch }) {
try {
const { data } = await messaging.send(ACTION.READY)
commit(types.SET_LOGGING_DATA, data) // capture this initial data to be used later
index.js
突變:
[types.SET_LOGGING_DATA] (state, isLoggingData) {
state.loggedData.initData = isLoggingData
},
App.vue
:
async created () {
localStorage.setItem('isLoggingData', this.isLoggingData)
await this.onCreated()
async onCreated () {
await this.init()
我不知道如何監聽localStorage.isLoggingData
的變化。
vuex-shared-mutations
應該滿足您的需求。
Share vuex mutations across tabs via `localStorage`.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.