You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionuseHistoryStateNavigation(base){const{ history, location }=windowletcurrentLocation={value: createCurrentLocation(base,location),}lethistoryState={value: history.state}if(!historyState.value){changeLocation(currentLocation.value,{back: null,current: currentLocation.value,forward: null,position: history.length-1,replaced: true,scroll: null,},true)}functionchangeLocation(to,state,replace){consturl=createBaseLocation()+// preserve any existing query when base has a hash(base.indexOf('#')>-1&&location.search
? location.pathname+location.search+'#'
: base)+totry{history[replace ? 'replaceState' : 'pushState'](state,'',url)historyState.value=state}catch(err){warn('Error with push/replace State',err)location[replace ? 'replace' : 'assign'](url)}}functionreplace(to,data){conststate=assign({},history.state,buildState(historyState.value.back,// keep back and forward entries but override current positionto,historyState.value.forward,true),data,{position: historyState.value.position})changeLocation(to,state,true)currentLocation.value=to}functionpush(to,data){constcurrentState=assign({},historyState.value,history.state,{forward: to,scroll: computeScrollPosition(),})if(!history.state){warn(`history.state seems to have been manually replaced without preserving the necessary values. Make sure to preserve existing history state if you are manually calling history.replaceState:\n\n`+`history.replaceState(history.state, '', url)\n\n`+`You can find more information at https://next.router.vuejs.org/guide/migration/#usage-of-history-state.`)}changeLocation(currentState.current,currentState,true)conststate=assign({},buildState(currentLocation.value,to,null),{position: currentState.position+1},data)changeLocation(to,state,false)currentLocation.value=to}return{location: currentLocation,state: historyState,
push,
replace
}}
在我们创建 router 对象的时候,会创建一个 history 对象,前面提到 Vue Router 支持三种模式,这里我们重点分析 HTML5 的 history 的模式:
对于 routerHistory 对象而言,它有两个重要的作用,一个是路径的切换,一个是监听路径的变化。
其中,路径切换主要通过 historyNavigation 来完成的,它是 useHistoryStateNavigation 函数的返回值,我们来看它的实现:
该函数返回的 push 和 replace 函数,会添加给 routerHistory 对象上,因此当我们调用 routerHistory.push 或者是 routerHistory.replace 方法的时候实际上就是在执行这两个函数。
push 和 replace 方法内部都是执行了 changeLocation 方法,该函数内部执行了浏览器底层的 history.pushState 或者 history.replaceState 方法,会向当前浏览器会话的历史堆栈中添加一个状态,这样就在不刷新页面的情况下修改了页面的 URL。
我们使用这种方法修改了路径,这个时候假设我们点击浏览器的回退按钮回到上一个 URL,这需要恢复到上一个路径以及更新路由视图,因此我们还需要监听这种 history 变化的行为,做一些相应的处理。
The text was updated successfully, but these errors were encountered: