Vue3 使用 Vue Router 时,prams 传参失效和报错问题

Discarded invalid param(s) "id", "name", "age" when navigating

我尝试使用 prams 传递数据

复制代码
<script setup>
import { useRouter } from 'vue-router'
    
const router = useRouter()
const params = { id: '1', name: 'ly', phone: 13246566476, age: 23 }
const toDetail = () => router.push({ name: 'detail', params })

</script>

<template>
  <el-button type="danger" @click="toDetail">查看情页</el-button>
</template>

在接收页面尝试渲染 prams 传递的数据:

复制代码
<template>
  <div>姓名:{{ route.params?.name }}</div>
  <div>电话:{{ route.params?.phone }}</div>
  <div>年龄:{{ route.params?.age }}</div>
</template>

<script setup>
import { useRoute } from 'vue-router'
const route = useRoute()
</script>

当我跳转页面时,得到的 prams 数据为空 ,控制台也出现了 Vue Router 的警告

点开链接后发现了原因, 点击查看更新日志

也就是说,从Vue Router的2022-8-22 这次更新后,我们使用上面的方式在新页面无法获取:

vue也给我们提出了解决方案:

  1. 使用 query 的方式传参

    只要改成query 传参就好了,注意query传参只能用路由表中的 path ,不是 name ,并且所有参数都会显示在URL 地址上。

    复制代码
    <script setup>
    import { useRouter } from 'vue-router'
        
    const router = useRouter()
    const query = { id: '1', name: 'ly', phone: 13246566476, age: 23 }
    const toDetail = () => router.push({  path: '/detail', query })
    
    </script>
    <template>
      <el-button type="danger" @click="toDetail">查看情页</el-button>
    </template>
  2. 将参数放在 pinia 或 vuex仓库里

  3. 使用动态路由匹配

  4. 传递 state,在新页面使用 History API 接收参数(#使用 History API 方式传递和接收)

  5. 使用 meta 原信息方式传递 (此方式更适用于路由守卫)

使用动态路由匹配

如果传递参数较少的情况下,可以尝试使用下面这种方式,只要修改一下path定义部分就可以了:

复制代码
// params 传递的参数: { id: '1', name: 'ly', phone: 13246566476, age: 23 }

{
      path: '/detail/:id/:name/:phone/:age',
      name: 'detail',
      component: () => import('@/views/detail/index.vue')
}

查看页面效果,控制台警告也消失了:

注意,如果使用使用了这种动态路由匹配方式, path: '/detail/:id/:name/:phone/:age' ,中这三个参数你都必须传递,否则会报错:

个人觉得这么写很鸡肋: 如果不把params参数写在路由路径中无法得到 params 参数,这种方式的params参数又会显示在地址栏。虽然不算弃用params, 但是每次把params参数写在路由路径中是很麻烦的一件事。

使用 History API 方式传递和接收

在跳转前的页面使用 state 参数:

复制代码
<script setup>
import { useRouter } from 'vue-router'
    
const router = useRouter()

const params = { id: '1', name: 'ly', phone: 13246566476, age: 23 }
const toDetail = () => router.push({ name: 'detail', state: { params } })

</script>

<template>
  <el-button type="danger" @click="toDetail">查看情页</el-button>
</template>

跳转的后页面接收:

复制代码
<template>
  <div>{{ historyParams }}</div>
</template>

<script setup lang="ts">

const historyParams = history.state.params
console.log('history.state', history.state)
</script>
相关推荐
threerocks3 分钟前
什么?我连 A2A、MCP 都没学会,现在又来了 AG-UI、A2UI.
前端·aigc·ai编程
牛奶31 分钟前
如何自己写一个浏览器插件?
前端·chrome·浏览器
亿元程序员1 小时前
为什么Cocos都4.0了还有人用2.x?
前端
MomentYY1 小时前
AI 到底是“懂”,还是在“猜”?
前端·人工智能·ai编程
鹏毓网络科技1 小时前
Cursor Rules 文件配置实战:3 个隐藏参数让我每月少写 40% 样板代码
前端·github
没烦恼3011 小时前
无痕模式下 HTTP\-First 拦截引发的“页面刷新”误判
前端
ZhengEnCi1 小时前
Q02-Vue-React-index.html完全指南
vue.js·react.js·html
文心快码BaiduComate1 小时前
从个人提效到组织提效:Comate辅助构建自我进化的AI研发系统
前端·程序员
hunterandroid2 小时前
Compose 状态管理:remember、rememberSaveable 与状态提升
前端
星栈2 小时前
Dioxus 接数据库最容易写歪的 3 个地方:sqlx + SQLite 怎么接才顺
前端·rust·前端框架