詳解從Vue-router到html5的pushState
最近在用vue的時候突然想到一個問題
首先,我們知道vue實現(xiàn)的單頁應用中一般不會去刷新頁面,因為刷新之后頁面中的vuex數(shù)據(jù)就不見了。
其次,我們也知道一般情況下,url變更的時候,比如指定location.href、history.push、replace等,頁面就會刷新。
那么問題來了,vue頁面的頁面跳轉時怎么實現(xiàn)的?沒刷新頁面么?沒刷新頁面,又要改變url,加載新內(nèi)容怎么做的?
去翻了一下vue-router的源碼,找到這樣一段
export class HTML5History extends History { ... push (location: RawLocation, onComplete?: Function, onAbort?: Function) { const { current: fromRoute } = this this.transitionTo(location, route => { pushState(cleanPath(this.base + route.fullPath)) handleScroll(this.router, route, fromRoute, false) onComplete && onComplete(route) }, onAbort) } replace (location: RawLocation, onComplete?: Function, onAbort?: Function) { const { current: fromRoute } = this this.transitionTo(location, route => { replaceState(cleanPath(this.base + route.fullPath)) handleScroll(this.router, route, fromRoute, false) onComplete && onComplete(route) }, onAbort) } ... }
再看看方法內(nèi)部
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) } }
答案就是html5在history中新增加的方法:pushState和replaceState。這兩個又是干啥的呢?(兩個十分類似,以下以pushState為例說明,區(qū)別和push與replace一致)
HTML5的pushState()
首先看看這個是干什么的
pushState方法就是向history中push一條記錄,更改頁面url,但是不刷新頁面,不刷新頁面,不刷新頁面。不刷新頁面,這點很關鍵,這和下面的操作很相似
window.location.href = window.location.href + '#a=b'
知道干嘛的了,再看看API怎么用的
history.pushState(state, title, url);
state是一個對象,具體內(nèi)容除了最大640KB之外沒有別的限制,比如在vue中是生成了一個key存進去了。若無特殊需要傳個null即可。這個state可以在history或者popstate的事件中看到
history中的
popstate中的
title這個參數(shù)目前沒什么用處,可能是給以后預留的參數(shù),暫時用null就好了
url很明顯,就是替換后的url了。url可以接受絕對地址和相對地址,設置絕對地址的時候,要保證域名和當前域名一致,否則匯報如下錯誤
Uncaught DOMException: Failed to execute 'pushState' on 'History': A history state object with URL 'https://www.baidu.com/' cannot be created in a document with origin 'https://mocard-aliyun1.chooseway.com:8443' and URL 'https://mocard-aliyun1.chooseway.com:8443/views/h5/indexasdasd'.
at History.pushState (https://aixuedaiimg.oss-cn-hangzhou.aliyuncs.com/static/m/js/alog/v1.0.0/alog.min.js:1:23259)
at <anonymous>:1:9
HTML5的popstate()
popstate與pushState相對應,主要在頁面url變更的時候觸發(fā),一般綁定在window對象下
window.addEventListener('popstate', e => { console.log('popstate', ) })
前面pushState中傳入的state對象,可以在這邊接收到,并根據(jù)需要去做一些處理。
說到這,vue-router是怎么實現(xiàn)頁面“刷新”但不刷新的就知道了吧。
vue-router就是利用pushState這個屬性,在頁面前進的時候動態(tài)改變history的內(nèi)容,添加一條記錄,接著location跟著改變。同時根據(jù)router前往的路由獲取對應的js資源文件并掛載到目標dom上實現(xiàn)頁面內(nèi)容的更新,但是頁面本身并沒有刷新。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
vue實現(xiàn)路由跳轉動態(tài)title標題信息
這篇文章主要介紹了vue實現(xiàn)路由跳轉動態(tài)title標題信息,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06django簡單的前后端分離的數(shù)據(jù)傳輸實例 axios
這篇文章主要介紹了django簡單的前后端分離的數(shù)據(jù)傳輸實例 axios,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05Vue計算屬性computed與方法methods的區(qū)別及說明
這篇文章主要介紹了Vue計算屬性computed與方法methods的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08