最近看vue-router源码的时候,发现在hash模式下push或replace这个path的时候,会先判断是否支持h5的PushState方法,如果支持就用新的,不支持才使用location.hash或location.replace。
我知道hash和history的优缺点,但是这里我们已经明确使用hash模式了,但是为什么源码中还是要去做判断,如果支持就用新方法?不太理解为什么这么做?这么做的优势是什么?
// hash.js
function pushHash (path) {
// 这里为什么要去判断????????
if (supportsPushState) {
pushState(getUrl(path))
} else {
window.location.hash = path
}
}
function replaceHash (path) {
// 这里为什么要去判断????????
if (supportsPushState) {
replaceState(getUrl(path))
} else {
window.location.replace(getUrl(path))
}
}
可能有人没理解题主的意思
我觉得跟优雅降级和可访问性和兼容这个没关系吧
具体的看这条记录
commit
feat: enhance hashHistory to support scrollBehavior是为了支持scrollBehavior
具体原因是
window.history.pushState
可以传参key
过去,这样每个url
历史都有一个key
,用key
保存了每个路由的位置信息