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

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>

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

相关推荐

  1. vue3

    2024-04-01 01:18:01       8 阅读
  2. vue3跳转及

    2024-04-01 01:18:01       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-01 01:18:01       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-01 01:18:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-01 01:18:01       20 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-01 01:18:01       20 阅读

热门阅读

  1. KaTex 常用公式编辑

    2024-04-01 01:18:01       16 阅读
  2. synchronized的使用方式

    2024-04-01 01:18:01       15 阅读
  3. 单台服务器(非集群节点)向Hadoop集群传输数据

    2024-04-01 01:18:01       16 阅读
  4. C++计算资本市场收益及成本分配数学方程

    2024-04-01 01:18:01       15 阅读
  5. nginx配置

    2024-04-01 01:18:01       15 阅读
  6. nginx如何清理页面缓存

    2024-04-01 01:18:01       14 阅读
  7. Linux进程的基本概念

    2024-04-01 01:18:01       15 阅读
  8. VPP添加接口IP地址

    2024-04-01 01:18:01       14 阅读
  9. Activity入门1

    2024-04-01 01:18:01       11 阅读