引注意: 路由表改变后要重启服务
方式 一:
通过params
1.路由表中
<Route path=' /user/:id ' component={User}></Route>
2.Link处
HTML方式
<Link to={ pathname:' /user/2 ',query:{name:jack},state:{value:123},search:'?sort=name',hash:'#the-hash',abc:'def'} activeClassName='active'>XXXX</Link>
JS方式
this.props.history.push( { pathname:' /user/2 ',query:{name:jack},state:{value:123},search:'?sort=name',hash:'#the-hash',abc:'def'})
3.user页面
通过 this.props.match.params.id 就可以接受到传递过来的参数(id)
方式 二:
通过query
前提:必须由其他页面跳过来,参数才会被传递过来,query传的参数是加密的
注:不需要配置路由表。路由表中的内容照常:<Route path='/user' component={User}></Route>
1.Link处
HTML方式
<Link to={{ pathname:' /user',query:{name:jack},state:{value:123},search:'?sort=name',hash:'#the-hash',abc:'def'}}>
JS方式
this.props.history.push({ pathname:' /user',query:{name:jack},state:{value:123},search:'?sort=name',hash:'#the-hash',abc:'def'})
2.user页面
this.props.location.query.name //jack
this.props.location.state.value //123
this.props.location.search //?sort=name
this.props.location.hash //#the-hash
this.props.location.abc //def (自定义参数)
方式 三:
通过state
同query差不多,只是属性不一样,state传的参数是加密的
1.Link 处
HTML方式:
<Link to={{ pathname:' /user',query:{name:jack},state:{value:123},search:'?sort=name',hash:'#the-hash',abc:'def'}}>
JS方式:
this.props.history.push({ pathname:' /user',query:{name:jack},state:{value:123},search:'?sort=name',hash:'#the-hash',abc:'def'})
2.user页面
this.props.location.state.value
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步