增加 登录路由
This commit is contained in:
parent
ea2b40c9bc
commit
b1ac2da3ec
@ -1,34 +1,67 @@
|
|||||||
// Composables
|
// 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 = [
|
const routes = [
|
||||||
{
|
{
|
||||||
path: '/',
|
path: "/",
|
||||||
component: () => import('@/layouts/default/Default.vue'),
|
component: () => import("@/layouts/default/Default.vue"),
|
||||||
children: [
|
children: [
|
||||||
{
|
{
|
||||||
path: '',
|
path: "",
|
||||||
name: 'Home',
|
name: "Home",
|
||||||
|
meta: {
|
||||||
|
auth: true,
|
||||||
|
},
|
||||||
// route level code-splitting
|
// route level code-splitting
|
||||||
// this generates a separate chunk (Home-[hash].js) for this route
|
// this generates a separate chunk (Home-[hash].js) for this route
|
||||||
// which is lazy-loaded when the route is visited.
|
// which is lazy-loaded when the route is visited.
|
||||||
component: () => import('@/views/Home.vue'),
|
component: () => import("@/views/Home.vue"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'auth/login',
|
path: "auth/login",
|
||||||
name: 'Login',
|
name: "Login",
|
||||||
// route level code-splitting
|
component: () => import("@/views/auth/Login.vue"),
|
||||||
// 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'),
|
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
]
|
];
|
||||||
|
|
||||||
const router = createRouter({
|
const router = createRouter({
|
||||||
history: createWebHistory(process.env.BASE_URL),
|
history: createWebHistory(process.env.BASE_URL),
|
||||||
routes,
|
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