vuex结合localStorage


  • local.js
export default {//处理localStorage
    get(name){
        return window.JSON.parse(window.localStorage.getItem(name));
    },
    set(name, content){
        window.localStorage.setItem(name,  window.JSON.stringify(content));
    }
}
  • store.js
import local from './public/local'
mutations:{
    fn_token(state, arg){
        state.token = arg;
        local.set('token', arg);
    }
},
  • App.vue
import {mapMutations} from 'vuex'
import local from '../public/local'
methods:{
    ...mapMutations({
        fn_token:'fn_token'
    }),
}
create(){
    this.fn_token(local.get('token'));
}

提示:sessionStorage同上理。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 需求:不同组件间共用同一数据,当一个组件将数据发生变化时,其他组件也可以响应该变化。 分析:vue无法监听loca...
    Y丶小琪阅读 9,794评论 1 7
  • 基于Vue的一些资料 内容 UI组件 开发框架 实用库 服务端 辅助工具 应用实例 Demo示例 element★...
    尝了又尝阅读 4,876评论 0 1
  • 简说Vue (组件库) https://github.com/ElemeFE/element" 饿了么出品的VUE...
    Estrus丶阅读 5,733评论 0 1
  • UI组件 element - 饿了么出品的Vue2的web UI工具套件 Vux - 基于Vue和WeUI的组...
    鲁大师666阅读 43,537评论 5 97
  • UI组件 element- 饿了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的组件库 m...
    35eeabfa0772阅读 8,499评论 7 12