vuex持久化存储

Posted o2i

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vuex持久化存储相关的知识,希望对你有一定的参考价值。

App.vue    //cache缓存永久缓存的变量

<script>
export default {
    name: "App",
    created() {
        this.persisted();
    },
    methods: {//持久化
        persisted() {
            if (sessionStorage.getItem("cache")) {
                this.$store.replaceState(
                    Object.assign(
                        {},
                        this.$store.state,
                        JSON.parse(sessionStorage.getItem("cache"))
                    )
                );
                sessionStorage.removeItem("cache");
            }
            //监听刷新
            addEventListener("beforeunload", () => {
                sessionStorage.setItem(
                    "cache",
                    JSON.stringify(this.$store.state.cache)
                );
            });
        },
    },
};
</script>
<style>
</style>

 

以上是关于vuex持久化存储的主要内容,如果未能解决你的问题,请参考以下文章

vuex-persist数据持久化存储

vuex-persist数据持久化存储

Vuex数据持久化存储

vuex持久化存储

解决vuex在页面刷新后数据丢失的问题

vuex持久化插件应用vuex-persistedstate