深入理解React Router:从原理到实践
上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人

2.2 browserHistory

2.2.1 创建browserHistory

browserHistory也叫浏览器历史对象,特点为其location的pathname、search等与浏览器中的window.location对象的各属性完全兼容。在现代浏览器中,使用browserHistory可获得最大的开发兼容性。由于开发兼容性良好,在Web浏览器场景中,browserHistory应作为首要考虑的history。创建browserHistory的方式为:

对于createBrowserHistory,也可传入如下history配置:

对于browserHistory,默认的跳转不会造成页面刷新,如果设置forceRefresh为true,则在跳转过程中会强制刷新页面。

keyLength表示历史栈中栈记录的key字符串的长度,默认为6。

如果在创建history的时候传入了basename,则通过createHref、history.push和history.replace等方法都会得到basename与path的拼接。

上例中的href将为/base/path?the=query#the-hash。