首页 > 其他 > 详细

vuex持久化存储

时间:2021-06-08 09:48:43      阅读:23      评论:0      收藏:0      [点我收藏+]

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持久化存储

原文:https://www.cnblogs.com/lv77/p/14861172.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!