main.js
import Vue from 'vue'
import './plugins/axios'
import App from './App.vue'
import router from './router'
import store from './store'
import './plugins/element.js'
Vue.config.productionTip = false //提示生产消息
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
router
index.js
Vue.use(VueRouter)
const routes = [
{
// 随便定义的首页
path: '/',
name: 'index',
component: login,
redirect: '/login' //重定向
},
{
// 登陆页
path: '/login',
name: 'login',
component: login
},
{
// admin 的路由
path: '/admin',
name: 'admin',
redirect: '/adminHome',
component: admin,
meta: {requireAuth: true}, //开启授权的页面
children: [
{
path: '/adminHome',
name: 'Hi! admin',
component: adminHome,
meta: {requireAuth: true},
children: [
{
path: '/adminHome',
name: 'admin 主页',
component: adminHome,
meta: {requireAuth: true},
}
]
}
]
}
]
const router = new VueRouter({
mode: 'history',//也就是会出现如下的URL:,URL中带有#号
base: process.env.BASE_URL, //
routes
})
export default router
/*
session 设置:
1. token
2. name
3. type
4. tid
5. sid
5. 系统信息 info
*/
router.beforeEach((to, from, next) => {
console.log(from.path + ' ====> ' + to.path)
if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
if (sessionStorage.getItem("token") === 'true') { // 判断本地是否存在token
next()
} else {
// 未登录,跳转到登陆页面
next({
path: '/login',
query: {redirect: to.fullPath}
})
}
} else {
// 不需要登陆权限的页面,如果已经登陆,则跳转主页面
if(sessionStorage.getItem("token") === 'true'){
console.log('检查拦截器配置,大概率出现漏网之鱼')
const t = sessionStorage.getItem("type")
next('/' + t);
}else{
next();
}
}
});
plugins
axios
import Vue from 'vue';
import axios from "axios";
// Full config: https://github.com/axios/axios#request-config
// axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
let config = {
// baseURL: process.env.baseURL || process.env.apiUrl || ""
// timeout: 60 * 1000, // Timeout
// withCredentials: true, // Check cross-site Access-Control
};
const _axios = axios.create(config);
//请求拦截
_axios.interceptors.request.use(
function(config) {
// Do something before request is sent
return config;
},
function(error) {
// Do something with request error
return Promise.reject(error);
}
);
// Add a response interceptor //相应拦截
_axios.interceptors.response.use(
function(response) {
// Do something with response data
return response;
},
function(error) {
// Do something with response error
return Promise.reject(error);
}
);
Plugin.install = function(Vue, options) {
Vue.axios = _axios;
window.axios = _axios;
Object.defineProperties(Vue.prototype, {
axios: {
get() {
return _axios;
}
},
$axios: {
get() {
return _axios;
}
},
});
};
Vue.use(Plugin)
export default Plugin;
store
index.js
import Vue from 'vue'
import './plugins/axios'
import App from './App.vue'
import router from './router'
import store from './store'
import './plugins/element.js'
Vue.config.productionTip = false //提示生产消息
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')