vue3中使用路由

1.安装

官网链接https://router.vuejs.org/installation.html#direct-download-cdn

npm install vue-router@4

2.创建文件夹

src\router\index.ts

// 1.导入
import { createRouter, createWebHashHistory } from 'vue-router'


const routes = [
    { path: '/', name: '', component: () => import('../components/Child.vue') },
    { path: '/Child2', component: () => import('../components/Child2.vue') },
]
const router = createRouter({
    history: createWebHashHistory(),
    routes, // short for `routes: routes
})



// 导出路由
export default router

3.引入main.ts

//程序的主入口文件,ts文件
//引入createApp函数,创建对应的应用,产生应用的实例对象
import { createApp } from 'vue'
//引入App组件(所有组件的父级组件)
import App from './App.vue'
// 引入路由
import router from './router'
//创建App应用返回对应的实例对象,调用mount方法进行挂载
const app = createApp(App)
// 使用路由
app.mount('#app')
app.use(router)


效果

在这里插入图片描述

4.动态路由带参,错误路由导错到404页

src\router\index.ts

// 1.导入
import { createRouter, createWebHashHistory } from 'vue-router'


const routes = [
    { path: '/news/:id', component: () => import('../components/News.vue') },
    { path: '/:path(.*)', component: () => import('../components/404.vue') },
]
const router = createRouter({
    history: createWebHashHistory(),
    routes, // short for `routes: routes
})



// 导出路由
export default router

在这里插入图片描述

效果
在这里插入图片描述

5.路由正则与重复参数

在id后面添加括号,正则校验参数值为数字,输错跳404

// 1.导入
import { createRouter, createWebHashHistory } from 'vue-router'


const routes = [
    { path: '/news/:id(\\d+)', component: () => import('../components/News.vue') },
    { path: '/:path(.*)', component: () => import('../components/404.vue') },
]
const router = createRouter({
    history: createWebHashHistory(),
    routes, // short for `routes: routes
})



// 导出路由
export default router

在这里插入图片描述
可重复参数

id后面加个+号

// 1.导入
import { createRouter, createWebHashHistory } from 'vue-router'


const routes = [
    { path: '/news/:id(\\d+)+', component: () => import('../components/News.vue') },
    { path: '/:path(.*)', component: () => import('../components/404.vue') },
]
const router = createRouter({
    history: createWebHashHistory(),
    routes, // short for `routes: routes
})



// 导出路由
export default router

效果
在这里插入图片描述

id后面加个*号,传或不传都可以

// 1.导入
import { createRouter, createWebHashHistory } from 'vue-router'


const routes = [
    { path: '/news/:id(\\d+)*', component: () => import('../components/News.vue') },
    { path: '/:path(.*)', component: () => import('../components/404.vue') },
]
const router = createRouter({
    history: createWebHashHistory(),
    routes, // short for `routes: routes
})



// 导出路由
export default router

在这里插入图片描述

6.嵌套路由

src\router\index.ts

// 1.导入
import { createRouter, createWebHashHistory } from 'vue-router'


const routes = [
    {
        path: '/news',
        children: [{
            path: 'child1',
            component: () => import('../components/Child1.vue')
        }, {
            path: 'child2',
            component: () => import('../components/Child2.vue')
        }],
        component: () => import('../components/News.vue')
    },
    { path: '/:path(.*)', component: () => import('../components/404.vue') },
]
const router = createRouter({
    history: createWebHashHistory(),
    routes, // short for `routes: routes
})



// 导出路由
export default router

src\components\News.vue

<template>
  <h4>新闻页嵌套路由</h4>
  <router-view />
</template>

<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
  name: "C_hild",
  setup() {
    return;
  },
});
</script>

src\components\Child1.vue

<template>
  <h4>子路由1</h4>
  <router-view />
</template>

<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
  name: "C_hild",
  setup() {
    return;
  },
});
</script>

src\components\Child2.vue

<template>
  <h4>子路由2</h4>
  <router-view />
</template>

<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
  name: "C_hild",
  setup() {
    return;
  },
});
</script>

效果
在这里插入图片描述

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3使用路由需要进行一些变化。首先,你需要导入路由API,可以通过以下方式导入: ```javascript import { useRouter } from 'vue-router' ``` 然后,你需要在组件使用`useRouter`函数来获取路由实例,例如: ```javascript const router = useRouter() ``` 接下来,你可以使用`router`对象来进行路由操作,比如跳转到其他路由: ```javascript router.push('/other-route') ``` 在Vue 3路由的目录结构和Vue 2一样,通常将路由相关的文件放在`src/router`目录下。其,`index.ts`是路由的入口文件,`routes.ts`是路由配置文件,可以将路由配置拆分成多个模块文件进行管理。另外,`main.ts`是项目的入口文件。\[1\]\[2\] 需要注意的是,在Vue 3,无法直接在组件使用`this.$route`来获取当前路由信息。你可以通过`router.currentRoute`来获取当前路由的信息,例如: ```javascript const currentRoute = router.currentRoute.value ``` 然后,你可以通过`currentRoute`对象来获取路由的相关信息,比如路径、参数等。\[3\] #### 引用[.reference_title] - *1* *2* *3* [vue3 路由使用](https://blog.csdn.net/m0_49515138/article/details/128870058)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值