博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
React中的url参数——this.props.match
阅读量:6871 次
发布时间:2019-06-26

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

有哪些参数

· this.props.match.match

· this.props.match.location

· this.props.match.history

也就是如果我们没用react-router中的route组件包裹组件,我们就需要通过上面形式获得url的信息,都是在match里面;而如果我们了react-router的route组件,我们就可以直接通过this.props.match,this.props.location,this.porps.history来获取url信息。

具体分析

在react组件的componentDidMount方法中打印一下this.props,控制台可以看到:

可以看到页面的URL信息全都包含在match字段中,以地址为例

localhost:3000/app/knowledgeManagement/modify/STY20171011124209535/3/1507701970070/0/?s=1&f=7复制代码

其中各个参数定义对应如下

localhost:3000/app/knowledgeManagement/modify/:studyNo/:stepId/:randomNum/:isDefault/?s=&f=复制代码

打印出this.props.match:

可以看到this.props.match中包含的URL信息是很丰富的,其中

  • history:包含了组件可以使用的各种路由系统的方法,常用的有 push 和 replace,两者都是跳转页面,但是 replace 不会引起页面的刷新,仅仅是改变 url。

  • location:相当于URL 的对象形式表示,通过 search 字段可以获取到 url 中的 query 信息。(这里 state 的含义与 HTML5 history.pushState API 中的 state 对象一样。每个 URL 都会对应一个 state 对象,你可以在对象里存储数据,但这个数据却不会出现在 URL 中。实际上,数据被存在了 sessionStorage 中)(参考: )

  • match:包含了具体的 url 信息,在 params 字段中可以获取到各个路由参数的值。

    通过以上分析,获取 url 中的指定参数就十分简单了,下面是几个例子

    // localhost:3000/app/knowledgeManagement/modify/STY20171011124209535/3/1507701970070/0/?s=1&f=7// localhost:3000/app/knowledgeManagement/modify/:studyNo/:stepId/:randomNum/:isDefault/?s=1&f=7// 获取 studyNothis.props.match.match.params.studyNo // STY20171011124209535// 获取 stepIdthis.props.match.match.params.stepId // 3// 获取 successconst query = this.props.match.location.search // '?s=1&f=7'const arr = query.split('&') // ['?s=', 'f=7']const successCount = arr[0].substr(3) // '1'const failedCount = arr[1].substr(2) // '7'复制代码

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

你可能感兴趣的文章
关于APP接口设计
查看>>
【VI】如何再执行上一个(历史)命令(已解决)
查看>>
KendoUI系列:DropDownList
查看>>
Axure7.0汉化方法
查看>>
我的MYSQL学习心得(九)
查看>>
JavaScript高级程序设计学习笔记--DOM
查看>>
Python易学就会(五)turtle绘制椭圆与递归
查看>>
echarts map地图设置外边框或者阴影
查看>>
webpack4学习总结
查看>>
vue.js vue-router history模式路由,域名二级目录子目录nginx配置
查看>>
web前端规范
查看>>
理解网页的关键渲染路径(CRP)
查看>>
使用vue-cli脚手架+webpack搭建vue项目
查看>>
Docker - 03 编排容器 Docker Compose 指令速查表
查看>>
Mybatis基本映射--INSERT
查看>>
移动 web 端屏幕适配 - rem
查看>>
聊聊hystrix的BucketedCounterStream
查看>>
50多种适合机器学习和预测应用的API,你的选择是?(2018年版本)
查看>>
【题目】【4天】寻宝
查看>>
Flutter教程(一) 十分钟了解Flutter
查看>>