|
@@ -4,11 +4,11 @@ import store from '@/store/index';
|
|
|
|
|
|
// 路由3+版本的异常处理
|
|
|
const originalPush = VueRouter.prototype.push
|
|
|
-VueRouter.prototype.push = function push(location) {
|
|
|
+VueRouter.prototype.push = function push (location) {
|
|
|
return originalPush.call(this, location).catch(err => err)
|
|
|
}
|
|
|
const originalReplace = VueRouter.prototype.replace;
|
|
|
-VueRouter.prototype.replace = function replace(location) {
|
|
|
+VueRouter.prototype.replace = function replace (location) {
|
|
|
return originalReplace.call(this, location).catch(err => err);
|
|
|
}
|
|
|
|
|
@@ -31,9 +31,9 @@ const routes = [
|
|
|
requireAuth: true, // 添加该字段,表示进入这个路由是需要登录的
|
|
|
},
|
|
|
component: main,
|
|
|
- children:[
|
|
|
+ children: [
|
|
|
{ // 首页
|
|
|
- path:'/',
|
|
|
+ path: '/',
|
|
|
component: r => require.ensure([], () => r(require('@views/index/index')), 'indexM')
|
|
|
},
|
|
|
...yRouter,
|
|
@@ -50,9 +50,9 @@ const routes = [
|
|
|
component: error404,
|
|
|
},
|
|
|
{
|
|
|
- path: '/login',
|
|
|
+ path: '/login',
|
|
|
meta: {
|
|
|
- requireAuth: true,
|
|
|
+ requireAuth: true,
|
|
|
},
|
|
|
name: 'login',
|
|
|
component: login,
|
|
@@ -72,18 +72,18 @@ const router = new VueRouter({
|
|
|
/**
|
|
|
* 路由的钩子函数,处理是否登录的判断
|
|
|
* **/
|
|
|
- router.beforeEach((to, from, next) => {
|
|
|
- // 登录后登录处理办法
|
|
|
- if(to.path === '/login' && store.getters.authToken){
|
|
|
- next({path: '/'})
|
|
|
- }else if (to.matched.some(m => m.meta.requireAuth)) { // 需要登录
|
|
|
- if(window.localStorage.authToken){
|
|
|
+router.beforeEach((to, from, next) => {
|
|
|
+ // 登录后登录处理办法
|
|
|
+ if (to.path === '/login' && store.getters.authToken) {
|
|
|
+ next({ path: '/' })
|
|
|
+ } else if (to.matched.some(m => m.meta.requireAuth)) { // 需要登录
|
|
|
+ if (window.localStorage.authToken) {
|
|
|
next()
|
|
|
} else if (to.path !== '/login') {
|
|
|
let token = localStorage.getItem('authToken');
|
|
|
- if (token === null || token === '' || token === undefined){
|
|
|
- next({path: '/login'})
|
|
|
- }else{next()}
|
|
|
+ if (token === null || token === '' || token === undefined) {
|
|
|
+ next({ path: '/login' })
|
|
|
+ } else { next() }
|
|
|
} else {
|
|
|
next()
|
|
|
}
|