关于页面跳转传递参数的问题

#1

A页面 有条目 显示 用户 个人信息(比如手机号 等等) 有一个查询按钮

点击后跳转到B页面

B页面有一个 serach框, 会显示A页面传过来的 手机号,
同时查询出这个手机号相关的信息。

请问这个方式怎么实现?

是在路由跳转的时候带 参数(手机号码过去吗)?

1 Like
#2

是的
你可以在点击时把URL设置为 url/${phoneNum}
然后router里写 path=“url/:phoneNum”
然后在B页面调用this.props.params.phoneNum

也可以A页面点击时 this.props.router.push({ query: { phoneNum: 138xx } )
B页面获取 this.props.location.query.phoneNum

还可以 this.props.router.push({ state: { phoneNum: 138xx } )
B页面获取 this.props.location.state.phoneNum

前两个会把 phoneNum 显示在地址栏里,后一个不会
但是用 state 传的话,状态只能保存在前后一个页面里

#3

我采用第二种方式,
import {router} from “react-router”;
this.props.router.push(’/notary/public’, {query: { collectCode: e }} );

Uncaught TypeError: Cannot read property ‘push’ of undefined

为什么会出现这种情况?不懂。

#4

额,抱歉
应该要先引用 import { withRouter } from ‘react-router’
然后用 withRouter 把这个组件包起来
export default withRouter( ComponentName)

#5

好的 我试试。

另外有一个问题,
我刚在看 https://react-guide.github.io/react-router-cn/

import {xxx} from ‘react-router’ 中
比如withRouter,IndexRoute, RouterContext 这一些的,有些词汇我都没有看到有解释的