方式一:使用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("/"))
}