}
使用window.scrollTo来进行滚动处理
if (position) {
window.scrollTo(position.x, position.y)
}
}
push
push操作也是 HTML5History模式下的一个比较关键的方法,他使用pushState来进行跳转操作,然后handleScroll来进行滚动
export function pushState (url?: string, replace?: boolean) {
<!-- 保存当前页面的滚动位置 -->
saveScrollPosition()
// try...catch the pushState call to get around Safari
// DOM Exception 18 where it limits to 100 pushState calls
const history = window.history
try {
<!-- 判断是哪种操作动作 -->
if (replace) {
history.replaceState({ key: _key }, '', url)
} else {
_key = genKey()
history.pushState({ key: _key }, '', url)
}
} catch (e) {
window.location[replace ? 'replace' : 'assign'](url)
}
}HashHistory实现
对于HashHistory的实现,和HTML5History的区别是在于Listener的方式和跳转的方式
Listener的方式这里是使用了hashchange,但是如果需要滚动行为就会去监听popstate
window.addEventListener(supportsPushState ? 'popstate' : 'hashchange')跳转的方式会判断是否需要滚动,不需要就直接使用window.location.hash
function pushHash (path) {
if (supportsPushState) {
pushState(getUrl(path))
} else {
window.location.hash = path
}
}总结
以上所述是小编给大家介绍的vue-router之hash模式和history模式,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对软件开发网网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!










