vue3 router路由传参给组件示例代码

本文介绍了在Vue应用中使用vue-router进行路径导航时如何传递参数,以及父子组件间的通信方法,如query和props的使用。同时展示了在Vue2组件中的数据管理与更新示例。
摘要由CSDN通过智能技术生成

components/HelloWorld.vue

<script setup>
import { ref } from 'vue'

import {useRoute} from 'vue-router'

const route = useRoute();

const query = route.query
// console.log(query)

</script>

<template>
	<router-link :to="{ 
		path: '/hello/person', 
		query: { id: 1, title: '跳转'} 
		}"><!--也可以采用param传参 -->
		跳转
		</router-link>
	<router-view/>
</template>
<style scoped>
a {
  color: #42b983;
}
</style>

routre/index.js

import Person from '../components/Person.vue'

import HelloWorld from '../components/HelloWorld.vue'

import {createRouter,createWebHistory} from 'vue-router'

import VueTwo from '../components/Vue.vue'

const router = createRouter({
	history:createWebHistory(),
	routes:[
		{
			path:'/hello',
			component:HelloWorld,
			children:[
				{
					path:'/hello/person',
					component:Person
				}
			]
		},
		{	//第二种传参形式2
			path:'/vue2',
			component:VueTwo,
			// props: (route) => ({ id: 1, title: 2, content:3 })
			props(route){
				return ({ id: 1, title: 2, content:3 })
			}
		}
	]
})

export default router

components/person.vue

<template>
	<div class="ttt">
		<h2>路由给到的数据:{{query}}</h2>
	</div>
</template>

<script setup lang="ts" name="testName">
	import { useRoute } from 'vue-router'
	
	const route = useRoute()
	console.log("person.vue页面获取参数:"+JSON.stringify(route.query))
</script>

<style>
	.ttt{
		color:red
	}
</style>

页面效果:
在这里插入图片描述
Vue.vue

<template>
	<div>
		<h2>{{nameAll}}</h2>
		<h2>{{method}}</h2>
		<h2>{{tt()}}</h2>
		<h2>{{firstName}}</h2>
		<h2>更新后赋值数据:{{lastName}}</h2>
		<h2>赋值数据:{{writeValue}}</h2>
		<button @click="tt">vue2数据更新</button>
	</div>
</template>

<script lang ='ts' name='VueTwo'>
	export default{
		data(){
			return {
				firstName:"wu",
				lastName:"liuqi"
			}
		},
		props: ['id', 'title', 'content'],
		mounted(){
			console.log('ID:', this.id);
			console.log('Title:', this.title);
			console.log('Content:', this.content);
		},
		computed:{
			nameAll:function(){
				return this.firstName + this.lastName
			},
			method(){
				return 111
			},
			writeValue:{
				get(){
					return this.firstName + this.lastName
				},
				set(value){
					this.lastName = value
					return value
				}
			}
		},
		methods:{
			tt(){
				this.writeValue = "alilailail"
			}
		}
	}
</script>

<style>
</style>

页面效果:
在这里插入图片描述

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3中,可以使用路由的params和query来传递参数。 1. 使用params传递参数:可以通过在<router-link>中设置params来传递参数。例如:<router-link :to="{name:'guest',params:{id:1}}">跳转到路由为guest页面</router-link>。在目标路由的组件中,可以使用useRoute()来获取参数。例如,在组件中使用useRoute()获取参数的示例代码如下: ```javascript <script setup lang="ts"> import { useRoute } from 'vue-router'; import { onMounted } from 'vue'; const route = useRoute(); onMounted(() => { console.log(route.params.id); // 输出1 }) </script> ``` 2. 使用query传递参数:可以通过在<router-link>中设置query来传递参数。例如:<router-link :to="{path:'guest',query:{id:1}}">跳转到路由为guest页面</router-link>。在目标路由的组件中,可以使用useRoute()来获取参数。例如,在组件中使用useRoute()获取参数的示例代码如下: ```javascript <script setup lang="ts"> import { useRoute } from 'vue-router'; import { onMounted } from 'vue'; const route = useRoute(); onMounted(() => { console.log(route.query.id); // 输出1 }) </script> ``` 综上所述,以上是在Vue3中使用路由传参的方法。 #### 引用[.reference_title] - *1* *2* *3* [Vue3路由传参](https://blog.csdn.net/qq_54334713/article/details/126721772)[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^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值