1.replace
replace是替换原来页面的过程
使用
this.props.history.replace()
2.push
push是一个 压栈的过程,因此它可以进行回退
使用
this.props.history.push()
3.案例
import React, { Component } from 'react'import {Link,Route} from 'react-router-dom'import Col from './Col'export default class index extends Component {state={result:[{id:"01",title:"我是关于1"},{id:"02",title:"我是关于2"},{id:"03",title:"我是关于3"},{id:"04",title:"我是关于4"}]}// replace方式replaceShow=(id,title)=>{return (e)=>{this.props.history.replace(`/about/col`,{id,title})}}//push的方式pushShow=(id,title)=>{return ()=>{this.props.history.push(`/about/col`,{id,title})}}render() {const {result}=this.stateconsole.log(result)return (<div><h1>about组件</h1><hr/><ul>{result.map(item=>{return(<li key={item.id}><Link to={{pathname:'/about/col',state:{id:item.id,title:item.title}}}>{item.title}</Link><button onClick={this.pushShow(item.id,item.title)}>push方式跳转</button><button onClick={this.replaceShow(item.id,item.title)}>replace方式跳转</button></li>)})}</ul><Route path='/about/col' component={Col}></Route></div>)}}
