vuex缓存

2018-05-05  本文已影响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);
如果需要详细的说明可以阅读官方文档!!!
上一篇下一篇

猜你喜欢

热点阅读