Vue权限控制addRoutes 发表于 2018-10-03 说到Vue权限控制,你可能会第一个想到123456789101112131415161718192021222324252627282930313233343536373839404142434445464748/** * Created by superman on 17/2/16. * http配置 */import axios from 'axios'import store from './store/store'import * as types from './store/types'import router from './router'// axios 配置axios.defaults.timeout = 5000;axios.defaults.baseURL = 'https://api.github.com';// http request 拦截器axios.interceptors.request.use( config => { if (store.state.token) { config.headers.Authorization = `token ${store.state.token}`; } return config; }, err => { return Promise.reject(err); });// http response 拦截器axios.interceptors.response.use( response => { return response; }, error => { if (error.response) { switch (error.response.status) { case 401: // 401 清除token信息并跳转到登录页面 store.commit(types.LOGOUT); router.replace({ path: 'login', query: {redirect: router.currentRoute.fullPath} }) } } // console.log(JSON.stringify(error));//console : Error: Request failed with status code 402 return Promise.reject(error.response.data) });export default axios; VueRouter12345678910111213141516const router = new VueRouter({ routes})router.beforeEach((to, from, next) => { if (to.path == '/login') { //清除token ... } let token = get('token'); //获取token if (!token && to.path != '/login') { next({ path: '/login' }) } else { next() }}) 但上面的只能对登录拦截,跳转拦截,不能控制user权限,最好的方式是动态注入路由addRoutes1234import router from './router'router.addRoutes(routes)或者this.$router.addRoutes(routers)