vue-router原理

通过改变 URL,在不重新请求页面的情况下,更新页面视图。

  • 1.默认 hash
  • 2. history。如果浏览器不支持 history 新特性,则采用 hash
  • 3. 如果不在浏览器环境下,就采用 abstract(Node环境下)

Hash

push() 方法最主要的是对 window 的 hash 进行了直接赋值:

window.location.hash=route.fullPath

replace()方法与push()方法不同之处在于,它并不是将新路由添加到浏览器访问历史的栈顶,而是替换掉当前的路由

window.location.replace(
    window.location.href.slice(0, i >= 0 ? i : 0) + '#' + path
)

为 hash 的改变添加监听事件来监听路由的变化

window.addEventListener("hashchange",funcRef,false)

 

History

window.history.pushState(stateObject,title,url)
window.history,replaceState(stateObject,title,url)

监听popState(window.onpopstate)来监听路由的变化
需要服务器配置不管通过何种子路由来直接访问,都需要能顺利拿到html,不然会出现404 

posted @ 2019-09-25 15:25  瑞瑞大人  阅读(330)  评论(0编辑  收藏  举报