博客
关于我
获取路由参数的几种方式
阅读量:457 次
发布时间:2019-03-06

本文共 2645 字,大约阅读时间需要 8 分钟。

React Router 路由参数处理指南

1. props.match.params

在 React Router 中,props.match.params 是一个常用的获取路由参数的方法。它能够直接从 URL 中提取路径参数,并将其作为组件的属性注入。

示例代码

import React from 'react';import { RouteConfigComponentProps } from 'react-router-config';interface IParam {  id?: string;}interface IProps extends RouteConfigComponentProps {} const Main = (props: IProps) => {  const { id } = props.match.params;  console.log('参数:', id);  return (    
{id &&

当前ID: {id}

}
);}export default Main;

注意事项

  • props.match.params 的类型需要手动声明,否则默认类型会是 {}
  • 可以通过接口定义自定义的路由参数类型

2. useParams Hook

useParamsreact-router-dom 提供的一个Hooks函数,能够直接返回路由参数对象。它与 props.match.params 的使用方式类似,但更加灵活,适用于组合式编程结构。

示例代码

import React from 'react';import { RouteConfigComponentProps } from 'react-router-config';import { useParams } from 'react-router-dom';interface IParam {  id?: string;}interface IProps extends RouteConfigComponentProps {} const Main = (props: IProps) => {  const { id } = useParams();  console.log('参数:', id);  return (    
{id &&

当前ID: {id}

}
);}export default Main;

注意事项

  • useParams 也需要指定参数的类型,否则默认类型会是 {}
  • 建议在使用组合路由时配合使用 useParams 以方便参数管理

3. props.location.search

props.location.search 是路由历史中的搜索部分,通常用于从 URL 中提取额外的查询参数。它可以用于处理动态路由参数或自定义的查询字符串。

示例代码

import qs from 'query-string';interface IProps extends RouteConfigComponentProps {} const A = (props: IProps) => {  const gotoPage = () => {    props.history.push({      pathname: '/B',      search: qs.stringify({ id: 123123 })    });  };  return (      );}export default A;

接收参数示例

import qs from 'query-string';interface IProps extends RouteConfigComponentProps {} const B = (props: IProps) => {  const { id } = qs.parse(props.location.search);  console.log('参数:', id);  return (    
{id &&

当前ID: {id}

}
);}export default B;

路由参数传递场景

A 页面跳转到 B 页面

在 A 页面中,通过 history.push 方法可以实现对 B 页面的跳转,并携带查询参数。

import qs from 'query-string';interface IProps extends RouteConfigComponentProps {} const A = (props: IProps) => {  const gotoPage = () => {    props.history.push({      pathname: '/B',      search: qs.stringify({ id: 123123 })    });  };  return (      );}export default A;

B 页面接收参数

在 B 页面中,通过 qs.parse 方法可以解析 URL 中的搜索参数。

import qs from 'query-string';interface IProps extends RouteConfigComponentProps {} const B = (props: IProps) => {  const { id } = qs.parse(props.location.search);  console.log('参数:', id);  return (    
{id &&

当前ID: {id}

}
);}export default B;

注意事项

  • 在使用 history.push 时,search 参数的传递需要使用 query-string 库进行 URL 编码
  • useParamsprops.match.params 在功能上是等价的,选择哪种方式取决于你的项目结构
  • 对路由参数进行类型声明有助于代码的可读性和维护性

转载地址:http://ankbz.baihongyu.com/

你可能感兴趣的文章
power English (3)原文
查看>>
POWER ENGLISH(7)- repetition
查看>>
SpringBoot中集成SpringBatch详细解析与实战示例(CSV文件读取十万条数据进行业务处理后写入Mysql数据库)
查看>>
powerbi 一张表在另外一张表中出现的数量_PowerBi之初步学习笔记
查看>>
QGIS怎样设置简体中文以及新建可编辑的多边形的图层
查看>>
PowerBuilder 使用自定义事件触发键盘Enter事件
查看>>
PowerCreatorCMS UploadResourcePic 任意文件上传漏洞复现
查看>>
PowerDesigner 使用的一些技巧(转)
查看>>
QGIS在Windows上下载安装与建立空间数据库连接
查看>>
PowerDesigner165安装婆姐汉花教程
查看>>
PowerDesigner使用教程:设置注释、默认值属性
查看>>
PowerDesigner使用教程:不显示背景网格
查看>>
PowerDesigner使用教程:创建数据模型以及导出
查看>>
PowerDesigner使用教程:右侧工具栏显示/隐藏
查看>>
PowerDesigner使用教程:导出sql文件以及解决中文乱码问题
查看>>
PowerDesigner使用教程:时间字段设置
查看>>
PowerDesigner使用教程:给字段添加唯一约束
查看>>
QGIS中怎样设置图层样式并导出地图样式
查看>>
PowerDesigner使用笔记
查看>>
QGIS中怎样实现数据坐标系转换
查看>>