我拥有在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
中的更改。
答案 0 :(得分:0)
vuex-shared-mutations
应该符合您的需求。
Share vuex mutations across tabs via `localStorage`.