vuex缓存

一、安装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);
如果需要详细的说明可以阅读官方文档!!!
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容