在react-router中组件里面的跳转可以用<Link>
但是在组件外面改如何跳转,需要用到react路由的history
1.首先是history的安装:
npm install history --save
2.我创建了一个JS文件,方便各个文件引用
history.js
import { createBrowserHistory } from 'history'
const history = createBrowserHistory({
basename: '', //基链接
forceRefresh: true //是否强制刷新
});
export default history
3.history实现跳转
push
使用push可以将一条新的历史记录推送到历史堆栈中
history.push('/a');
history.push('/a',{name: 'jac'});
history.push({
pathname: '/a',
state: {
name: 'jac'
}
});
replace方法和push方法使用形式一样,replace的作用是取代当前历史记录
go,此方法用来前进或者倒退,history.go(-1);
goBack,此方法用来回退,history.goBack();
goForward,此方法用来前进,history.goForward();