
上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人
2.2.3 history监听
browserHistory的browserHistory.listen方法监听browserHistory的location的改变,通过hisotry.push、history.replace、history.go等方法改变location,都会触发回调监听函数。各方法除了能获得最新导航的location地址,还能获取到不同的跳转类型action。其调用方式如下:

history.listen的回调函数仅在产生导航、location发生变化时被触发,页面初始加载不会被触发。在页面初始加载后,页面地址会同步到history.location中。注意,这里监听location“变化”并不表示前后导航过程中location的值不一致发生变化,当前后导航过程中产生的location一致时,同样会触发history.listen的回调函数。
如果希望在history.listen的回调函数中获取上一次浏览记录,则可通过声明外部变量实现:

