【发布时间】:2022-01-21 18:44:55
【问题描述】:
我使用 npm install --save vuex-persistedstate 安装了持久状态。
然后我像这样设置我的 Vuex store.js 文件:
import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)
export const store = new Vuex.Store({
state: {
title: ''
},
mutations: {},
actions: {},
getters: {},
plugins: [createPersistedState()]
})
..在另一个组件中,我将值传递给 store.title:
this.$store.state.title = this.input
而且它有效。
但在我刷新页面 (F5) 后,this.input 不再存储在this.$store.state.title 中,它破坏了我的网站功能。
我做错了什么?
编辑:我得到了真正的香草风格:
第一:
this.$store.state.title = this.input
localStorage.setItem('title', this.$store.state.title)
然后:
mounted () {
this.$store.state.title = localStorage.getItem('title')
}
还是想知道,为什么 Vuex 插件不起作用:(
【问题讨论】:
-
您能否验证浏览器中localStorage 中的数据是否正确更新?
-
只有键
loglevel:webpack-dev-server和值:WARN
标签: javascript vue.js npm vuejs2 vuex