【发布时间】:2019-08-22 09:43:45
【问题描述】:
我在使用 Vuex mapState 时遇到了范围问题。我需要在页面上本地输出用户名,而不会被我的输入字段中的 v-model 响应更新(对于相同的用户名数据)。
以下代码可以正常工作,但是如果用户刷新页面,状态会丢失并返回为未定义。
computed: {
...mapState(["userProfile"]),
},
beforeMount() {
this.localUserProfile.name = this.userProfile.name;
console.log("localUserProfile: " + this.localUserProfile.name);
},
【问题讨论】:
-
您应该将 vuex 状态存储在 localStorage(或 sessionStorage)中,以防您想在页面刷新后保留它。您可以使用
vuex-persistedstate或类似的插件