export default class Message extends Component { state = { messageArr: [ { id: '1', title: '消息1' }, { id: '2', title: '消息2' }, { id: '3', title: '消息3' } ] } handleReplace = (id, title) => { this.props.history.replace(`/home/message/detail/${id}/${title}`) } handlePush = (id, title) => { this.props.history.push(`/home/message/detail/${id}/${title}`) } handeleForward = () => { this.props.history.goForward() } handleBack = () => { this.props.history.goBack() } handleGo = () => { this.props.history.go(2) } render() { return ( <div> <ul> { this.state.messageArr.map((msgObj) => { return ( <li key={msgObj.id}> {/* 向路由组件传递params传递参数 */} <Link to={"/home/message/detail/" + msgObj.id + "/" + msgObj.title}>{msgObj.title}</Link> <button onClick={() => this.handlePush(msgObj.id, msgObj.title)}>push查看</button> <button onClick={() => this.handleReplace(msgObj.id, msgObj.title)}> replace查看</button> {/* 向路由组件传递search传递参数 */} {/* <Link to={`/home/message/detail/?id=${msgObj.id}&title=${msgObj.title}`} >{msgObj.title}</Link> */} {/* 向路由组件传递state传递参数 */} {/* <Link to={{ pathname: "/home/message/detail", state: { id: msgObj.id, title: msgObj.title } }} >{msgObj.title}</Link> */} </li> ) }) } <hr /> {/* 声明接收params参数 */} <Route path="/home/message/detail/:id/:title" component={Detail} /> <button onClick={this.handeleForward}>前进</button> <button onClick={this.handleBack}>后退</button> <button onClick={this.handleGo}>go</button> {/* 声明接收search参数 ,无需声明接收,正常注册理由即可*/} {/* <Route path="/home/message/detail" component={Detail} /> */} {/* 声明接收state参数 ,无需声明接收,正常注册理由即可*/} {/* <Route path="/home/message/detail" component={Detail} /> */} </ul> </div> ) } }
备注:使用search和state传参也都一样可以