web前端全系列 教程
1839个小节阅读:2244.1k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
很多时候,我们需要将给定匹配模式的路由映射到同一个组件。例如,我们可能有一个 User
组件,它应该对所有用户进行渲染,但用户 ID 不同。在 Vue Router 中,我们可以在路径中使用一个动态字段来实现,我们称之为 路径参数
xxxxxxxxxx
// router/index.js
const routes = [
{
path:"/",
component:Home
}
...
{
path:"/details/:name",
component:Details
}
]
xxxxxxxxxx
<template>
<h3>列表</h3>
<ul>
<li v-for="(item,index) in user.names" :key="index">
<router-link :to="'/details/' + item">{{ item }}</router-link>
</li>
</ul>
</template>
<script setup>
import { reactive } from "vue"
const user = reactive({
names:["iwen","ime","frank"]
})
</script>
xxxxxxxxxx
<template>
<h3>详情</h3>
<p>{{ name }}</p>
</template>
<script>
export default {
data(){
return{
name:""
}
},
mounted(){
this.name = this.$route.params.name
}
}
</script>
xxxxxxxxxx
<template>
<h3>详情</h3>
<p>{{ name }}</p>
</template>
<script setup>
import { useRoute } from "vue-router"
import { ref } from "vue"
const route = useRoute()
const name = ref("")
name.value = route.params.name;
</script>
xxxxxxxxxx
{
path:"/details/:name?",
component:Details
}
实时效果反馈
1.在Vue路由中,可以获取页面跳转传递的参数是:
A router
B params
C this.params
D this.$route.params
答案
1=>D