美文网首页
vuex缓存

vuex缓存

作者: 闹闹也会有脾气 | 来源:发表于2018-05-05 14:20 被阅读0次

    一、安装vuex

    运行命令 npm install vuex --save
    

    二、新建一个js文件


    image.png

    命名为store.js,代码如下

    import Vue from 'vue'
    import Vuex from 'vuex'
    
    Vue.use(Vuex);
    
    const store = new Vuex.Store({
      // 定义状态
      state: {
        /**
         * user 对象 -- 我的业务需求只需要缓存user对象即可
         */
    
        user: {}
      }
    });
    
    export default store
    
    

    三、注册main.js

    import Vue from 'vue'
    import App from './App'
    import Vuex from 'vuex'
    import store from './common/store/store'
    
    Vue.use(Vuex);
    
    /* eslint-disable no-new */
    new Vue({
      el: '#app',
      store,
      components: {App},
      template: '<App/>',
      render: h => h(App)
    });
    
    

    四、方法使用

    Set方法
    this.$store.state.user = {test:"test"}; //注意你缓存声明的数据格式
    
    Get方法
    let user = this.$store.state.user;
    alert(user.test);
    
    如果需要详细的说明可以阅读官方文档!!!
    

    相关文章

      网友评论

          本文标题:vuex缓存

          本文链接:https://www.haomeiwen.com/subject/cbasrftx.html