最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解從Vue-router到html5的pushState

 更新時(shí)間:2018年07月21日 15:45:54   作者:木子墨  
這篇文章主要介紹了詳解從Vue-router到html5的pushState,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

最近在用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è)對象,具體內(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可以接受絕對地址和相對地址,設(shè)置絕對地址的時(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相對應(yīng),主要在頁面url變更的時(shí)候觸發(fā),一般綁定在window對象下

window.addEventListener('popstate', e => {
 console.log('popstate', )
})

前面pushState中傳入的state對象,可以在這邊接收到,并根據(jù)需要去做一些處理。

說到這,vue-router是怎么實(shí)現(xiàn)頁面“刷新”但不刷新的就知道了吧。

vue-router就是利用pushState這個(gè)屬性,在頁面前進(jìn)的時(shí)候動(dòng)態(tài)改變history的內(nèi)容,添加一條記錄,接著location跟著改變。同時(shí)根據(jù)router前往的路由獲取對應(yīng)的js資源文件并掛載到目標(biāo)dom上實(shí)現(xiàn)頁面內(nèi)容的更新,但是頁面本身并沒有刷新。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

金寨县| 腾冲县| 喀什市| 前郭尔| 稷山县| 盈江县| 泰安市| 大邑县| 成安县| 舞钢市| 阳西县| 洪雅县| 广南县| 桃园县| 河北区| 高碑店市| 滁州市| 霍山县| 东乡县| 高要市| 宁远县| 丰镇市| 平陆县| 石棉县| 瑞安市| 绥中县| 汽车| 攀枝花市| 高淳县| 元朗区| 峨山| 灌南县| 赤城县| 湾仔区| 炉霍县| 扶沟县| 南通市| 重庆市| 天水市| 宣恩县| 白银市|