实现一个简单的 Vuex 状态持久化功能

Oct 9, 20181 min read

This post was last modified 2010 days ago, and some of the content may be outdated.

大多数人应该都遇到过 vuexstate 刷新后丢失的问题,这是因为 state 是存储在内存中的,刷新后当然就丢失了。 这里有个方案是每次更改 state 后把 state 存储在 localStorage 中,当初始化 vuex 时再从 localStorage 中取出。

使用 vuex 插件功能可以很方便实现上述方案

JS

以上只是一个简单实现,项目中建议使用 vuex-persistedstate

HITS

LAST UPDATED

Oct 9, 2018
Made withbyXiaojun