039、Vue3+TypeScript基础,路由中使用redirect来重定向

01、index.ts代码如下:

//创建路由并暴露出去
import {createRouter, createWebHistory} from 'vue-router'
import Home from '@/view/Home.vue'
import About from '@/view/About.vue'
import News from '@/view/News.vue'
import Detail from '@/view/Detail.vue'

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {name: 'zhuye', path: '/home', component: Home},
        {name: 'guanyu', path: '/about', component: About},
        {
            name: 'xinwen', path: '/news', component: News,
            //子类的path不需要加斜杠
            children: [
                {
                    // 一定要使用name
                    name: 'neirong',
                    path: 'detail',
                    component: Detail,
                    props(route) {
                        return route.query
                    },
                },
            ]
        },
        {
            path: '/',
            redirect: '/home'
        }
    ]
})

export default router

02、效果如下:

 

posted @ 2024-08-21 16:53  像一棵海草海草海草  阅读(84)  评论(0编辑  收藏  举报