1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| import { createRouter,createWebHistory } from 'vue-router' import type { RouteRecordRaw,RouterOptions,Router } from 'vue-router'
//RouterRecordRaw路由組件對象 import Layout from '../layout/Layout.vue';
import { createRouter,createWebHistory } from 'vue-router' import type { RouteRecordRaw, RouterOptions, Router } from 'vue-router' import Layout from '../layout/Layout.vue'; const routes: Array<RouteRecordRaw> = [ { path: '/', name: 'Home', component: () => import('../views/Home.vue') }, { path: '/about_us', name: 'About Us', component: () => import("../views/AboutUs.vue"), }, { path: '/admin/', redirect: '/admin/index', //重新導向 component: Layout, children: [ { path: "index", component: () => import("../views/Dashboard/Admin.vue"), name: "Admin", meta: { title: "admin", requiresAuth: true }, }, ] }, //**404頁面 =>當沒有這個頁面時就會導到此頁 { path: '/:catchAll(.*)', name: '404', //views資料夾內必須要有errorPage/404.vue component: () => import('../views/errorPage/404.vue'), meta: { title: '404' }, } ]
// RouterOptions是路由選項類型 const options: RouterOptions = { // history: createWebHashHistory(), history: createWebHistory(),//井字號會不顯示 routes, }
// Router是路由對象類型 const router: Router = createRouter(options)
//路由守衛 router.beforeEach((to, from, next) => { const teacherId: any = localStorage.teacherId; const talkId: any = localStorage.talkId; const isLogin: Boolean =localStorage.token? true : false; if(to.path === "/admin" || to.path === "/login"|| to.path === "/index" || to.path==="/teachers" || to.path === "/user_login" || to.path === "/register" || to.path===`/teacher/${teacherId}` || to.path==='/onetoone' || to.path===`/talk/${talkId}`){ next() } else { isLogin ? next() : next("/index") ; } }) //輸出router export default router
|