最近做项目,需要登录拦截,验证。于是使用了axios的拦截器(也是第一次使用,摸索了1天,终于搞出来了,真是太高兴啦!!!),废话不多说,直接上代码,

项目结构:vue-cli + webpack + vue + vue-router + vuex

项目结构截图:

axios 拦截 , 页面跳转, token 验证(自己摸索了一天搞出来的)

其中src下store文件夹中有两个文件,store.js和type.js,其中store.js为:

 1 /**
 2  * Created by yuwenjing on 17/9/13.
 3  */
 4 import Vuex from 'vuex'
 5 import Vue from 'vue'
 6 import * as types from './types'
 7 
 8 Vue.use(Vuex);
 9 export default new Vuex.Store({
10     state: {
11         token: null,
12         userName: ''
13     },
14     mutations: {
15         [types.LOGIN]: (state, data) => {
16             localStorage.token = data;
17             state.token = data;
18         },
19         [types.LOGOUT]: (state) => {
20             localStorage.removeItem('token');
21             state.token = null
22         },
23         [types.USERNAME]: (state, data) => {
24             localStorage.userName = data;
25             state.userName = data;
26         }
27     }
28 })
View Code

相关文章:

  • 2021-05-04
  • 2022-12-23
  • 2022-12-23
  • 2022-02-16
  • 2022-01-12
  • 2021-12-12
  • 2022-12-23
  • 2021-05-08
猜你喜欢
  • 2021-06-07
  • 2022-12-23
  • 2021-12-11
  • 2022-12-23
  • 2021-11-18
相关资源
相似解决方案