詳解從Vue-router到html5的pushState
最近在用vue的時(shí)候突然想到一個(gè)問題
首先,我們知道vue實(shí)現(xiàn)的單頁應(yīng)用中一般不會(huì)去刷新頁面,因?yàn)樗⑿轮箜撁嬷械膙uex數(shù)據(jù)就不見了。
其次,我們也知道一般情況下,url變更的時(shí)候,比如指定location.href、history.push、replace等,頁面就會(huì)刷新。
那么問題來了,vue頁面的頁面跳轉(zhuǎn)時(shí)怎么實(shí)現(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。這兩個(gè)又是干啥的呢?(兩個(gè)十分類似,以下以pushState為例說明,區(qū)別和push與replace一致)
HTML5的pushState()
首先看看這個(gè)是干什么的
pushState方法就是向history中push一條記錄,更改頁面url,但是不刷新頁面,不刷新頁面,不刷新頁面。不刷新頁面,這點(diǎn)很關(guān)鍵,這和下面的操作很相似
window.location.href = window.location.href + '#a=b'
知道干嘛的了,再看看API怎么用的
history.pushState(state, title, url);
state是一個(gè)對(duì)象,具體內(nèi)容除了最大640KB之外沒有別的限制,比如在vue中是生成了一個(gè)key存進(jìn)去了。若無特殊需要傳個(gè)null即可。這個(gè)state可以在history或者popstate的事件中看到
history中的
popstate中的
title這個(gè)參數(shù)目前沒什么用處,可能是給以后預(yù)留的參數(shù),暫時(shí)用null就好了
url很明顯,就是替換后的url了。url可以接受絕對(duì)地址和相對(duì)地址,設(shè)置絕對(duì)地址的時(shí)候,要保證域名和當(dāng)前域名一致,否則匯報(bào)如下錯(cuò)誤
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相對(duì)應(yīng),主要在頁面url變更的時(shí)候觸發(fā),一般綁定在window對(duì)象下
window.addEventListener('popstate', e => { console.log('popstate', ) })
前面pushState中傳入的state對(duì)象,可以在這邊接收到,并根據(jù)需要去做一些處理。
說到這,vue-router是怎么實(shí)現(xiàn)頁面“刷新”但不刷新的就知道了吧。
vue-router就是利用pushState這個(gè)屬性,在頁面前進(jìn)的時(shí)候動(dòng)態(tài)改變history的內(nèi)容,添加一條記錄,接著location跟著改變。同時(shí)根據(jù)router前往的路由獲取對(duì)應(yīng)的js資源文件并掛載到目標(biāo)dom上實(shí)現(xiàn)頁面內(nèi)容的更新,但是頁面本身并沒有刷新。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue系列之requireJs中引入vue-router的方法
- vue-router中的hash和history兩種模式的區(qū)別
- vue-router中scrollBehavior的巧妙用法
- vue-router history模式下的微信分享小結(jié)
- vue-router 源碼實(shí)現(xiàn)前端路由的兩種方式
- vue-router 源碼之實(shí)現(xiàn)一個(gè)簡(jiǎn)單的 vue-router
- vue-router+nginx 非根路徑配置方法
- 使用vue-router完成簡(jiǎn)單導(dǎo)航功能【推薦】
- 使用vue-router beforEach實(shí)現(xiàn)判斷用戶登錄跳轉(zhuǎn)路由篩選功能
相關(guān)文章
對(duì)類Vue的MVVM前端庫的實(shí)現(xiàn)代碼
這篇文章主要介紹了對(duì)類Vue的MVVM前端庫的實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09vue實(shí)現(xiàn)路由跳轉(zhuǎn)動(dòng)態(tài)title標(biāo)題信息
這篇文章主要介紹了vue實(shí)現(xiàn)路由跳轉(zhuǎn)動(dòng)態(tài)title標(biāo)題信息,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06django簡(jiǎn)單的前后端分離的數(shù)據(jù)傳輸實(shí)例 axios
這篇文章主要介紹了django簡(jiǎn)單的前后端分離的數(shù)據(jù)傳輸實(shí)例 axios,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05Vue計(jì)算屬性computed與方法methods的區(qū)別及說明
這篇文章主要介紹了Vue計(jì)算屬性computed與方法methods的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08