react withRouter的用法

withRouter的作用就是, 如果我们某个东西不是一个Router, 但是我们要依靠它去跳转一个页面, 比如点击页面的logo, 返回首页, 这时候就可以使用withRouter来做.withRouter, 作用是将一个组件包裹进Route里面, 然后react-router的三个对象history, location, match就会被放进这个组件的props属性中.(我的理解加上之后可以写编程时导航,不想vue可以在全局用this.$router.push()来完成)

import React from 'react';
import { connect } from 'dva';
import  styles from  './nav.css'
import { Route, Switch, routerRedux,withRouter } from 'dva/router';dva写法
//import { withRouter } from 'react-router-dom';//普通react项目写法

class Nav extends React.Component {
  
    jumping(){//跳转到
      this.props.history.push('/')
     console.log(this.props)
    }
    render() {
        return (
            <div className={styles.box}>
              <div className={styles.navBox}>
                 <div className={styles.iconBox}>
                   <div className={styles.iconImg}></div>
                   <div className={styles.title}>公路地质灾害时空数据监测预警系统</div>
                 </div>
                 <div className={styles.listBox}>
                 <div className={styles.listItem} onClick={this.jumping.bind(this)}>动态多维可视化子系统</div>
                 <div className={styles.listItem}>灾害多源监测数据存储子系统</div>
                 <div className={styles.listItem}>灾害监测预警子系统</div>
                 <div className={styles.listItem}>多源信息智能服务子系统</div>
                 </div>
              </div>
            </div>
        );
    }
}
export default withRouter(Nav);

这个例子中点击div利用history跳转到别的页面。没加withRouter时,this.props里没有history,加上之后才有。

如果withRouter和connect一起用一定把withRouter写外面。

export default   withRouter(connect(({ menu, mapindex }) => ({ menu, mapindex }))(TopMenu));


 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

崽崽的谷雨

漫漫前端路,摸爬滚打

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值