增加 登录路由
This commit is contained in:
parent
ea2b40c9bc
commit
b1ac2da3ec
@ -1,34 +1,67 @@
|
||||
// Composables
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
import { createRouter, createWebHistory } from "vue-router";
|
||||
import { useUserStore } from "@/store/user";
|
||||
import { useConfigStore } from "@/store/config";
|
||||
|
||||
const routes = [
|
||||
{
|
||||
path: '/',
|
||||
component: () => import('@/layouts/default/Default.vue'),
|
||||
path: "/",
|
||||
component: () => import("@/layouts/default/Default.vue"),
|
||||
children: [
|
||||
{
|
||||
path: '',
|
||||
name: 'Home',
|
||||
path: "",
|
||||
name: "Home",
|
||||
meta: {
|
||||
auth: true,
|
||||
},
|
||||
// route level code-splitting
|
||||
// this generates a separate chunk (Home-[hash].js) for this route
|
||||
// which is lazy-loaded when the route is visited.
|
||||
component: () => import('@/views/Home.vue'),
|
||||
component: () => import("@/views/Home.vue"),
|
||||
},
|
||||
{
|
||||
path: 'auth/login',
|
||||
name: 'Login',
|
||||
// route level code-splitting
|
||||
// this generates a separate chunk (Home-[hash].js) for this route
|
||||
// which is lazy-loaded when the route is visited.
|
||||
component: () => import('@/views/auth/Login.vue'),
|
||||
path: "auth/login",
|
||||
name: "Login",
|
||||
component: () => import("@/views/auth/Login.vue"),
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
];
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(process.env.BASE_URL),
|
||||
routes,
|
||||
})
|
||||
});
|
||||
|
||||
export default router
|
||||
router.beforeEach((to, from) => {
|
||||
|
||||
// get route name
|
||||
console.log(from.name, from.fullPath)
|
||||
|
||||
const userStore = useUserStore();
|
||||
const configStore = useConfigStore();
|
||||
|
||||
// if (to.matched.length === 0) {
|
||||
// return router.push({ name: "errors.404" });
|
||||
// }
|
||||
|
||||
if (to.meta.title) {
|
||||
document.title = to.meta.title + " - " + configStore.getAppName();
|
||||
} else {
|
||||
document.title = configStore.getAppName();
|
||||
}
|
||||
|
||||
if (to.meta.auth == true) {
|
||||
// validate login state
|
||||
if (userStore.jwt_token == null) {
|
||||
router.push({ path: "auth/login" });
|
||||
}
|
||||
} else {
|
||||
document.title = configStore.getAppName();
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
Loading…
Reference in New Issue
Block a user