import { createRouter, createWebHistory } from 'vue-router'
import store from "../stores/user";
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: '/',
name: 'home',
component: () => import('../layout/Layout.vue'),
redirect: '/home',
children: [
{
path: 'home',
name: 'Home',
component: () => import('../views/HomeView.vue'),
},
{
path: 'xiao',
name: 'xiao',
component: () => import('../views/Xiao.vue'),
},
{
path: 'commodity',
name: 'commodity',
component: () => import('../views/Commodity.vue'),
},
]
}
,
{
path: '/login',
name: 'Login',
component: () => import('../views/Login.vue'),
}
,
{
path: '/person',
name: 'Person',
component: () => import('../views/Person.vue'),
}
]
})
//全局路由守卫 beforeEach:路由改变都会触发
router.beforeEach((to, from, next) => {
const user = JSON.parse(localStorage.getItem('user'))
console.log(user)
if (to.path == "/login") {
next()
}else if (user != null){
next()
} else {
next("/login")
}
})
export default router
简单路由router及路由守卫
最新推荐文章于 2023-12-26 18:37:50 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)