react路由-react路由的跳转以及路由信息

课程目标

  1. 熟悉掌握路由的配置
  2. 熟悉掌握跳转路由的方式
  3. 熟悉掌握路由跳转传参的方式
  4. 可以根据对其的理解封装一个类似Vue的router-view

知识点

  1. 路由的配置属性
  • 路由配置属性主要有 path、name、component 等。
    path:组件相对路径
    name:组件路径别名
    component:组件地址
    在路由配置中,有两个属性 exact 和 strict 表示路径匹配。
    “/detail” exact 属性为 true 时匹配的路由有:"/detail/",
    但是 url 中有 “/detail/” 匹配不到。
    “/detail” strict 属性为 true 时匹配的路由有:"/detail",
    但是 “/detail/” 可以被匹配到。
    综上所述,要想严格匹配,就需要将 exact 和 strict 都设为 true
  1. 路由的跳转形式
  • Link 或者 NavLink形式,实质是个 a 标签。区别是后者可以切换时改变样式,用法如下:

<ul>
  <li><NavLink exact to="/" activeStyle={{
           fontWeight: 'bold',
           color: 'red'
         }}>home</NavLink>
  </li>
  <li><NavLink exact to="/detail" activeStyle={{
          fontWeight: 'bold',
          color: 'red'
         }}>detail</NavLink>
  </li>
</ul>

  • 使用点击事件,用法如下:

<ul>
  <li onClick={() => this.props.history.push({pathname:'detail'})}><div>home</div>
  </li>
  <li onClick={() => this.props.history.push({pathname:'home'})}><div>detail</div>
  </li>
</ul>

  1. 跳转传参
  • 直接Link属性传递

<ul>
  <li><NavLink exact to="/" activeStyle={{
           fontWeight: 'bold',
           color: 'red'
         }}>home</NavLink>
  </li>
  <li><NavLink exact to={{pathname:'detail',state:{id:2}}} activeStyle={{
          fontWeight: 'bold',
          color: 'red'
         }}>detail</NavLink>
  </li>
</ul>

  • 事件跳转传参

<ul>
  <li onClick={() => this.props.history.push({pathname:'detail',state:{id:1}})}><div>home</div>
  </li>
  <li onClick={() => this.props.history.push({pathname:'home',state:{
  id:0}})}><div>detail</div>
  </li>
</ul>

// 获取参数
this.props.history.location.state

  1. 传递的参数分类
1.使用query传递  
   this.props.history.push({ pathname : '/access_control/role/roleEdit',query:{ aa: 'aa'} })
 获取参数:
   this.props.location.query.aa
2.使用state传递
 this.props.history.push({ pathname : '/access_control/role/roleEdit',state:{ aa: 'aa'} })
 获取参数:
   this.props.location.state.aa
 
 
  state传的参数是加密的,query传的参数是公开的,在地址栏
  1. 封装RouterView
class RouterView extends Component {
    render () {
        let {
            routes
        } = this.props;
        let redir = routes && routes.length > 0 && routes.filter((v, i) => {
            return v.redirect
        })
        let renderRedir = redir.length > 0 && redir.map((v, i) => {
            return <Redirect from={v.path} to={v.redirect} key={i} />
        })
        return (
            <Switch>
                {
                    routes && routes.length > 0 && routes.map((v, i) => {
                        if (v.component) {
                            return <Route key={i} path={v.path} component={(props) => {
                                return <v.component routes={v.children} {...props} />
                            }} />
                        }
                    }).concat(renderRedir)
                }
            </Switch>
            
        )
路由跳转、路由信息.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. 后端路由与前端路由 多页面应用和后端路由在传统的Web应用中,浏览器根据地址栏的URL向服务器发送一个HTT...
    ImmortalSummer阅读 4,522评论 0 1
  • react-router React路由,让父组件动态去挂载不同的子组件,本文以4.x为例;4.x 对依赖包的划分...
    hellomyshadow阅读 4,272评论 0 3
  • React路由 一、路由的形式 hash路由 : HashRouter history路由 : BrowserRo...
    learninginto阅读 2,435评论 0 8
  • 一、vue路由 1、路由配置文件 (1)在router.js中导入Vue和VueRouter (2)Vue调用Vu...
    风的记忆乀阅读 12,899评论 0 58
  • 久违的晴天,家长会。 家长大会开好到教室时,离放学已经没多少时间了。班主任说已经安排了三个家长分享经验。 放学铃声...
    飘雪儿5阅读 12,186评论 16 22