通过params传递
export default class Message extends Component { state = { messageArr: [ { id: '1', title: '消息1' }, { id: '2', title: '消息2' }, { id: '3', title: '消息3' } ] } 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> </li> ) }) } <hr /> {/* 声明接收params参数 */} <Route path="/home/message/detail/:id/:title" component={Detail} /> </ul> </div> ) } }
const DetailData = [ { id: '1', content: '你好,北京' }, { id: '2', content: '你好,上海' }, { id: '3', content: '你好,广东' } ] export default class Detail extends Component { render() { console.log(this.props); // 接收params参数 const { id, title } = this.props.match.params const findResult = DetailData.find((detailObj) => { return detailObj.id === id }) return ( <div> <li>Id:{id}</li> <li>Title:{title}</li> <li>Content:{findResult.content}</li> </div> ) } }
通过Search传递路由参数
export default class Message extends Component { state = { messageArr: [ { id: '1', title: '消息1' }, { id: '2', title: '消息2' }, { id: '3', title: '消息3' } ] } 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> */} {/* 向路由组件传递search传递参数 */} <Link to={`/home/message/detail/?id=${msgObj.id}&title=${msgObj.title}`} >{msgObj.title}</Link> </li> ) }) } <hr /> {/* 声明接收params参数 */} {/* <Route path="/home/message/detail/:id/:title" component={Detail} /> */} {/* 声明接收search参数 ,无需声明接收,正常注册理由即可*/} <Route path="/home/message/detail" component={Detail} /> </ul> </div> ) } }
const DetailData = [ { id: '1', content: '你好,北京' }, { id: '2', content: '你好,上海' }, { id: '3', content: '你好,广东' } ] export default class Detail extends Component { render() { console.log(this.props); // 接收params参数 // const { id, title } = this.props.match.params //接收search参数 const { search } = this.props.location const { id, title } = qs.parse(search.slice(1)) const findResult = DetailData.find((detailObj) => { return detailObj.id === id }) return ( <div> <li>Id:{id}</li> <li>Title:{title}</li> <li>Content:{findResult.content}</li> </div> ) } }备注:获取到的search是urlencoded编码字符串,需要借助querystring进行解析 通过state传递路由参数
export default class Message extends Component { state = { messageArr: [ { id: '1', title: '消息1' }, { id: '2', title: '消息2' }, { id: '3', title: '消息3' } ] } 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> */} {/* 向路由组件传递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} /> */} {/* 声明接收search参数 ,无需声明接收,正常注册理由即可*/} {/* <Route path="/home/message/detail" component={Detail} /> */} {/* 声明接收state参数 ,无需声明接收,正常注册理由即可*/} <Route path="/home/message/detail" component={Detail} /> </ul> </div> ) } }
const DetailData = [ { id: '1', content: '你好,北京' }, { id: '2', content: '你好,上海' }, { id: '3', content: '你好,广东' } ] export default class Detail extends Component { render() { console.log(this.props); // 接收params参数 // const { id, title } = this.props.match.params //接收search参数 // const { search } = this.props.location // const { id, title } = qs.parse(search.slice(1)) //接收state参数 const { id, title } = this.props.location.state || {} const findResult = DetailData.find((detailObj) => { return detailObj.id === id }) || {} return ( <div> <li>Id:{id}</li> <li>Title:{title}</li> <li>Content:{findResult.content}</li> </div> ) } }