/chapters?courseId=32 vue3中路由和传参应该怎么写
时间: 2024-02-21 07:59:11 浏览: 54
在 Vue3 中使用 Vue Router 可以按照以下步骤配置路由和传递参数:
1. 安装 Vue Router
```bash
npm install vue-router@next
```
2. 在 main.js 中引入 Vue Router
```javascript
import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import App from './App.vue'
const router = createRouter({
history: createWebHistory(),
routes: [
{
path: '/chapters',
component: () => import('./views/Chapters.vue')
}
]
})
const app = createApp(App)
app.use(router)
app.mount('#app')
```
3. 创建一个名为 Chapters.vue 的视图组件,用于显示章节信息
```vue
<template>
<div>Chapters of course {{ courseId }}</div>
</template>
<script>
export default {
name: 'Chapters',
props: {
courseId: {
type: String,
required: true
}
}
}
</script>
```
4. 在路由中传递参数
```javascript
const router = createRouter({
history: createWebHistory(),
routes: [
{
path: '/chapters',
component: () => import('./views/Chapters.vue'),
props: route => ({ courseId: route.query.courseId })
}
]
})
```
这里通过 props 属性将路由中的参数传递给组件,使用 route.query.courseId 获取传递的参数。
现在你就可以在浏览器中访问 `/chapters?courseId=32`,看到页面上显示 "Chapters of course 32"。
阅读全文
相关推荐
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)