2021-03-25 18:29:58 +08:00

39 lines
918 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// index.js
import { createRouter, createWebHashHistory } from "vue-router"
import layout from '@/layout/index.vue'
import login from './modules/login'
import home from './modules/home'
import user from './modules/user'
import { TOKEN } from '@/store/modules/app'
const router = createRouter({
history: createWebHashHistory(),
routes: [
{
path: '/',
component: layout,
redirect: '/home',
children: [
...home,
...user
]
},
...login
],
});
// vue-router4的路由守卫不再是通过next放行而是通过return返回false或者一个路由地址
router.beforeEach((to, from) => {
if (!window.localStorage[TOKEN] && to.name !== 'login') {
return {
name: 'login',
query: {
redirect: to.path // redirect是指登录之后可以跳回到redirect指定的页面
},
replace: true
}
}
})
export default router;