vue的路由一般都是怎么写的

router/index.ts

第一种

import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/home/index.vue'
import CallView from '../callback.vue'
import LogoutView from '../logout.vue'
import silentView from '../silent.vue'
import AboutView from '../views/about/index.vue'

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            redirect: '/home'
        },
        {
            path: '/home',
            name: 'home',
            component: HomeView
        },
        {
            path: '/about',
            name: 'about',
            component: AboutView
        },
        {
            path: '/callback',
            name: 'Callback',
            component: CallView
        },
        {
            path: '/logout',
            name: 'Logout',
            component: LogoutView
        },
        {
            path: '/silent',
            name: 'Silent',
            component: silentView
        },{
            path: '/404',
            component: () => import("@/views/404.vue"),
          },
          {
            path:"/:pathMatch(.*)",
            redirect:'/404'
        }
    ],
    strict: false,
    scrollBehavior: () => ({ left: 0, top: 0 })
})

router.beforeEach((to, from, next) => {
    document.body.scrollTop = 0
    // firefox
    document.documentElement.scrollTop = 0
    next()
})

export default router

第二种

import { createRouter, createWebHistory } from "vue-router";

const baseRouter = []

const otherRouter = [
    {
        path: '/',
        redirect: "/home"
    },
    {
        path: "/home",
        name: 'home',
        component: () => import("@/views/home/index.vue"),
        meta: {
            title: "首页"
        }
    },
    {
        path: '/meeting',
        name: 'meeting',
        component: () => import("@/views/meeting/index.vue"),
        meta: {
            title: "会议通知"
        }
    },
    {
        path: "/meeting/details",
        name: "meetingDetails",
        component: () => import("@/views/meeting/details/index.vue"),
        meta: {
            title: "会议详情"
        }
    },
    {
        path: '/training',
        name: 'training',
        component: () => import("@/views/training/index.vue"),
        meta: {
            title: "培训通知"
        }
    },
    {
        path: "/training/details",
        name: "trainingDetails",
        component: () => import("@/views/training/details/index.vue"),
        meta: {
            title: "培训详情"
        }
    },
    {
        path: '/videos',
        name: 'videos',
        component: () => import("@/views/videos/index.vue"),
        meta: {
            title: "视频展示"
        }
    },
    {
        path: "/videos/details",
        name: "videosDetails",
        component: () => import("@/views/videos/details/index.vue"),
        meta: {
            title: "视频详情"
        }
    },
    {
        path: '/news',
        name: 'news',
        component: () => import("@/views/news/index.vue"),
        meta: {
            title: "新闻资讯"
        }
    },
    {
        path: "/news/details",
        name: "newsDetails",
        component: () => import("@/views/news/details/index.vue"),
        meta: {
            title: "资讯详情"
        }
    },
    {
        path: "/center",
        name: 'center',
        component: () => import("@/views/center/index.vue"),
        meta: {
            title: "中心介绍"
        }
    },
    {
        path: '/search',
        name: 'search',
        component: () => import('../views/search/index.vue')
      },
    {
        path: "/callback",
        name: "callback",
        component: () => import("@/views/callback.vue"),
    },
    {
        path: "/logout",
        name: "logout",
        component: () => import("@/views/logout.vue"),
    },
    {
        path: "/silent",
        name: "silent",
        component: () => import("@/views/silent.vue"),
    },
    {
        path: '/404',
        component: () => import("@/views/404.vue"),
      },
      {
        path:"/:pathMatch(.*)",
        redirect:'/404'
    }
]

const routes = [...baseRouter, ...otherRouter]

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes,
    strict: false,
    scrollBehavior: () => ({ left: 0, top: 0 })
});

export default router;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值