初学vue3与ts:路由跳转带参数

index-router

html 复制代码
<!-- 路由跳转 -->
<template>
	<div>
		<div class="title-sub flex">
			<div>1、用router-link跳转带参数id=1:</div><router-link to="./link?id=1"><button>点我跳转</button></router-link>
		</div>
		<div class="title-sub flex">
			<div>2、用router.push跳转,用query带参数name=lin:</div>
			<button @click="queryLink">点我跳转</button>
		</div>
		<div class="title-sub pl-60">
			注:用router.push的query参数,router->index.ts为【path: '/link',】即可,router.push可用name也可用path
		</div>
		<div class="title-sub flex">
			<div>3、用router.push跳转,用params带参数age=666:</div>
			<button @click="paramsLink">点我跳转</button>
		</div>
		<div class="title-sub pl-60">
			注:用router.push的params参数,需要在router->index.ts里更改【path: '/link/:age?',name:'link'】,(ps:age后面的?代表这个参数可传可不传),router.push要用name
		</div>
	</div>
</template>

<script lang="ts" setup>
	import { useRouter } from 'vue-router';
	const router = useRouter();
	// query
	function queryLink(){
		router.push({path:'/link',query:{name:'lin'}})
	}
	// params
	function paramsLink(){
		router.push({name:'link',params:{age:'666'}})
	}
</script>

<style>
	.index-title{
		font-size: 24px;
		font-weight: bold;
	}
	.title{
		font-weight: bold;
		font-size: 20px;
		padding-top: 20px;
		padding-left: 20px;
	}
	.title-sub{
		padding-left: 40px;
		margin-top: 10px;
	}
	.flex{
		display: flex;
		align-items: center;
	}
	.pl-60{
		padding-left: 60px !important;
	}
</style>

index-link

html 复制代码
<!-- 路由 - 目标页面 -->
<template>
	<div>
		<div class="title-sub flex" v-if="linkData">
			<div>用router-link跳转拿到参数:{{linkData}}</div>
		</div>
		<div class="title-sub flex" v-if="linkQuery">
			<div>用router.push跳转,用query带参数,拿到的参数是:{{linkQuery}}</div>
		</div>
		<div class="title-sub flex" v-if="linkAge">
			<div>用router.push跳转,用params带参数,拿到的参数是:{{linkAge}}</div>
		</div>
	</div>
</template>

<script lang="ts" setup>
	import { ref,onMounted } from 'vue'
	import { useRouter } from 'vue-router';
	const route = useRouter();
	const linkData = ref<any>('')
	const linkQuery = ref<any>('')
	const linkAge = ref<any>('')
	onMounted(()=>{
		console.log("route:",route)
		console.log("route.currentRoute:",route.currentRoute)
		console.log("route.currentRoute.value:",route.currentRoute.value)
		console.log("route.currentRoute.value.query:",route.currentRoute.value.query)
		// 用router-link跳转带参数id=1
		if(route.currentRoute.value.query.id){
			linkData.value = route.currentRoute.value.query.id
		}
		// 用router.push跳转,用query带参数name=lin
		if(route.currentRoute.value.query.name){
			linkQuery.value = route.currentRoute.value.query.name
		}
		// 用router.push跳转,用params带参数age=666
		if(route.currentRoute.value.params.age){
			linkAge.value = route.currentRoute.value.params.age
		}
	})
</script>

<style>
	.index-title{
		font-size: 24px;
		font-weight: bold;
	}
	.title{
		font-weight: bold;
		font-size: 20px;
		padding-top: 20px;
		padding-left: 20px;
	}
	.title-sub{
		padding-left: 40px;
		margin-top: 10px;
	}
	.flex{
		display: flex;
		align-items: center;
	}
</style>

【用router.push跳转,用params带参数age=666】这个方法的router->index.ts

js 复制代码
import { RouteRecordRaw, createRouter, createWebHistory } from 'vue-router'
 
// 静态路由表
const routes: Array<RouteRecordRaw> = [
	{
	    path: '/link/:age?',
		name:'link',
	    component: () => import('../views/home/index-link.vue')
	}
]
 
// 路由对象
const router = createRouter({
    history: createWebHistory(),
    routes
})
 
export default router

其他2中方法的router->index.ts

js 复制代码
{
   path: '/link',
   component: () => import('../views/home/index-link.vue')
}

ps :vue3目标页面要拿到上一页面带过来的参数,不能用route.query ,要用route.currentRoute.value.query 或者route.currentRoute.value.params,是以前可以用,现在变不一样了吗?有大佬知道告知下。谢谢!

相关推荐
于慨21 小时前
Lambda 表达式、方法引用(Method Reference)语法
java·前端·servlet
石小石Orz21 小时前
油猴脚本实现生产环境加载本地qiankun子应用
前端·架构
从前慢丶21 小时前
前端交互规范(Web 端)
前端
像我这样帅的人丶你还21 小时前
别再让JS耽误你进步了。
css·vue.js
@yanyu66621 小时前
07-引入element布局及spring boot完善后端
javascript·vue.js·spring boot
CHU72903521 小时前
便捷约玩,沉浸推理:线上剧本杀APP功能版块设计详解
前端·小程序
GISer_Jing21 小时前
Page-agent MCP结构
前端·人工智能
王霸天21 小时前
💥别再抄网上的Scale缩放代码了!50行源码教你写一个永不翻车的大屏适配
前端·vue.js·数据可视化
小领航21 小时前
用 Three.js + Vue 3 打造炫酷的 3D 行政地图可视化组件
前端·github
@大迁世界21 小时前
2026年React大洗牌:React Hooks 将迎来重大升级
前端·javascript·react.js·前端框架·ecmascript