当前位置: 首页 > news >正文

【Vue-Router】路由传参

1. query 传参

在这里插入图片描述
list.json

{"data": [{"name": "面","price":300,"id": 1},{"name": "水","price":400,"id": 2},{"name": "菜","price":500,"id": 3}]}

login.vue

<template><h1>我是列表页面</h1><table cellpadding="0" class="table" border="1"><thead><tr><th>商品</th><th>价格</th><th>操作</th></tr></thead><tbody><tr :key="item.id" v-for="item in data"><th>{{ item.name }}</th><th>{{ item.price }}</th><th><button @click="toDetail(item)">详情</button></th></tr></tbody></table>
</template><script setup lang="ts">
import { data } from './list.json'
import { useRouter } from 'vue-router';const router = useRouter()type Item = {name: string;price: number;id: number;
}const toDetail = (item: Item) => {router.push({path: '/reg',query: {id: item.id,name: item.name,price: item.price}})
}
</script><style scoped>
.table {width: 400px;
}
</style>

reg.vue

<template><h1>我是列表页面</h1><button @click="router.back">返回</button><div style="font-size: 20px;">品牌:{{ route.query.name }}</div><div style="font-size: 20px;">价格:{{ route.query.price }}</div><div style="font-size: 20px;">id: {{ route.query.id }}</div>
</template><script setup lang="ts">
import { useRoute } from 'vue-router';
import { useRouter } from 'vue-router';const router = useRouter();
const route = useRoute();</script><style scoped>
.reg {background-color: green;height: 400px;width: 400px;font-size: 20px;color: white;
}
</style>

App.vue

<template><h1>hello world</h1><hr><router-view></router-view>
</template><script setup lang="ts">
import { useRouter } from 'vue-router';
const router = useRouter();</script><style scoped></style>

在这里插入图片描述
在这里插入图片描述

2. 动态路由参数

index.ts

import { createRouter, createWebHistory, RouteRecordRaw, createWebHashHistory } from "vue-router";const routes: Array<RouteRecordRaw> = [{path: "/",name: 'Login',component: () => import("../components/login.vue")},{path: "/reg/:id",name: 'Reg',component: () => import("../components/reg.vue")}
]const router = createRouter({history: createWebHistory(),routes
})export default router

reg.vue

<template><h1>我是列表页面</h1><button @click="router.back()">返回</button><div style="font-size: 20px;">品牌:{{ item?.name }}</div><div style="font-size: 20px;">价格:{{ item?.price }}</div><div style="font-size: 20px;">id: {{ item?.id }}</div>
</template><script setup lang="ts">
import { useRoute } from 'vue-router';
import { useRouter } from 'vue-router';
import { data } from './list.json';const router = useRouter();
const route = useRoute();
// 返回对象用item接收
const item = data.find(v => v.id === Number(route.params.id))</script><style scoped>
.reg {background-color: green;height: 400px;width: 400px;font-size: 20px;color: white;
}
</style>

item?.name ,item?.price ,item?.id,他们如果不使用可选链操作符会出现报错:'__VLS_ctx.item' is possibly 'undefined'.

login.vue

<template><h1>我是列表页面</h1><table cellpadding="0" class="table" border="1"><thead><tr><th>商品</th><th>价格</th><th>操作</th></tr></thead><tbody><tr :key="item.id" v-for="item in data"><th>{{ item.name }}</th><th>{{ item.price }}</th><th><button @click="toDetail(item)">详情</button></th></tr></tbody></table>
</template><script setup lang="ts">
import { data } from './list.json'
import { useRouter } from 'vue-router';const router = useRouter()type Item = {name: string;price: number;id: number;
}const toDetail = (item: Item) => {router.push({// name 对应 router 的 namename: 'Reg',// 不会展示在URL上,存在于内存里params: {id: item.id}})
}
</script><style scoped>
.table {width: 400px;
}
</style>

在这里插入图片描述

http://www.lryc.cn/news/123844.html

相关文章:

  • 平板选择什么电容笔比较好?ipad手写笔推荐品牌
  • 什么是数字化车间
  • 创新零售,京东重新答题?
  • 面向对象设计与分析40讲(20)消息驱动编程和事件驱动编程模型
  • 【c语言】指针进阶(超详细)
  • C++入门篇8---vector
  • 【学会动态规划】最大子数组和(19)
  • 怎么做Tik Tok海外娱乐公会呢?新加坡市场怎么样?
  • mysql主从复制搭建
  • Java:正则表达式案例:爬数据,重复数据替换,数据分割
  • CF 765D Artsem and Saunders 构造
  • DevOps系列文章 之 SpringBoot整合GitLab-CI实现持续集成
  • K8S系列二:实战入门
  • form中表单切换,导致 relus 中的事件无法触发,原因:页面切换不要一直切换DOM,会导致问题,需要都显示出来
  • Android Ble蓝牙App(五)数据操作
  • .netcore grpc双向流方法详解
  • 【Servlet】(Servlet API HttpServlet 处理请求 HttpServletRequest 打印请求信息 前端给后端传参)
  • java中右移>>和无符号右移>>>的区别
  • 牛客周赛 Round 7
  • R语言生存分析(机器学习)(1)——GBM(梯度提升机)
  • k8s和docker简单介绍
  • Lua学习记录
  • 三分钟完美解决你的C盘内存过大爆红
  • C++ - equal(比较两个vector元素)
  • 多线程:线程池
  • 9.3.2.2网络原理(传输层TCP)
  • ssm+mybatis无法给带有下划线属性赋值问题
  • 学习笔记-JVM监控平台搭建
  • 使用css实现时间线布局(TimeLine)
  • 深入浅出 栈和队列(附加循环队列、双端队列)