dva中路由跳转的几种方式

方式一:使用Link标签

步骤一:从dva/router中引入Link

//1.引入Link
import { Link } from "dva/router"

步骤二:在jsx中使用Link标签,to属性中添加跳转路径

 {/* 2.添加一个Link标签 */}
<Link to="/">去首页</Link>

方式二:通过history.push

步骤一:如添加一个button按钮,并添加点击事件

<button onClick = { this.goToHomePage }>去首页</button>

步骤二:定义方法
注:执行此步骤先打印一下是否有history对象,只有被route标签直接包裹的组件才会有history对象,如果没有可以通过this.props传递history

    goToHomePage = (event) => {
        console.log(this.props);
        this.props.history.push("/")
    }

在这里插入图片描述
不传递history可以使用withRouter高阶函数:
步骤一:引入withRouter

import { withRouter } from "dva/router"

步骤二:导出组件

export default withRouter(组件名)

步骤三:定义方法

    goToHomePage = (event) => {
        console.log(this.props);
        this.props.history.push("/")
    }

在这里插入图片描述

方式三:使用routerRedux

步骤一:引入routerRedux

import { routerRedux } from "dva/router"

步骤二:如添加一个按钮,添加点击事件

 <button onClick={ this.goToHomePageRedux }>Redux去首页</button>

步骤三:定义事件

    goToHomePageRedux = (event) => {
        this.props.dispatch(routerRedux.push("/"))
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值