您的位置:首页 > Web前端 > Vue.js

vue将数据保存到vuex中

2020-08-21 20:57 579 查看

在项目中遇到这样一个问题,就是在登入的时候同时需要从后台获取到左边的导航,但是如果使用h5的localStorage来保存导航信息,会出现app加载进去之后localStorage才保存进浏览器,在mounted方法中,console.log(window.localStorage.getItem(‘xx’)打印出来是undefined,于是把数据保存到vuex中管理,

先新需要在项目中安装vuex:
运行命令 npm install vuex --save-dev
在项目的入口js文件main.js中
import store from ‘./store/index’
并将store挂载到vue上

import Vue from "vue";import Vuex from "vuex";

import createPersistedState from "vuex-persistedstate"Vue.use(Vuex);

export default new Vuex.Store({
state: {
active:0,
footer_list: [{
name: "首页",
path: '/',
icon: "#icon-niu1"},

{
name: "课程",
path: 'course',
icon: "#icon-shu"

},
{
name: "约课记录",
path: 'record',
icon: "#icon-ditu-shouzhi"
},
{
name: "练习",
path: 'practise',
icon: "#icon-lianxiben"
},
{
name: "我的",
path: 'person',
icon: "#icon-My"
}    ]  },
mutations: {
active(state,index){     state.active = index    }  },
actions: {},  modules: {},
plugins: [createPersistedState()]});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: