最近在用vue的时候突然想到一个问题

首先,我们知道vue实现的单页应用中一般不会去刷新页面,因为刷新之后页面中的vuex数据就不见了。

其次,我们也知道一般情况下,url变更的时候,比如指定location.href、history.push、replace等,页面就会刷新。

那么问题来了,vue页面的页面跳转时怎么实现的?没刷新页面么?没刷新页面,又要改变url,加载新内容怎么做的?

去翻了一下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)
}
...
}

再看看方法内部

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为例说明,区别和push与replace一致)

HTML5的pushState()

首先看看这个是干什么的

pushState方法就是向history中push一条记录,更改页面url,但是不刷新页面,不刷新页面,不刷新页面。不刷新页面,这点很关键,这和下面的操作很相似

window.location.href = window.location.href + '#a=b'

知道干嘛的了,再看看API怎么用的

history.pushState(state, title, url);
  • state是一个对象,具体内容除了最大640KB之外没有别的限制,比如在vue中是生成了一个key存进去了。若无特殊需要传个null即可。这个state可以在history或者popstate的事件中看到

    history中的



    popstate中的

  • title这个参数目前没什么用处,可能是给以后预留的参数,暂时用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变更的时候触发,一般绑定在window对象下
window.addEventListener('popstate', e => {
console.log('popstate', )
})

前面pushState中传入的state对象,可以在这边接收到,并根据需要去做一些处理。

说到这,vue-router是怎么实现页面“刷新”但不刷新的就知道了吧。

vue-router就是利用pushState这个属性,在页面前进的时候动态改变history的内容,添加一条记录,接着location跟着改变。同时根据router前往的路由获取对应的js资源文件并挂载到目标dom上实现页面内容的更新,但是页面本身并没有刷新。

最新文章

  1. OGG-01820 Could not enable workspace
  2. 关于jquery中html()、text()、val()的区别
  3. web应用程序传递连接字符串给FastReport数据源
  4. sqoop导出工具
  5. poj 2516 (费用流)
  6. Linux/UNIX数据文件和信息系统
  7. ecshop 属性表(attribute)商品属性表(goods_attr)货品表(prduct) 商品数量的联系
  8. Error creating bean with name &#39;com.you.user.dao.StudentDaoTest&#39;: Injection of autowired dependencies
  9. PYTHON 词云
  10. golang使用通道模仿实现valatile语义
  11. Unity 从StreamingAssets文件夹和PersistentData文件夹 保存读取AssetBundle资源
  12. 第31月第19天 NV12
  13. maven添加jetty插件,同时运行多个实例
  14. 二.hadoop环境搭建
  15. P5241 序列(滚动数组+前缀和优化dp)
  16. Go Example--协程
  17. 廖雪峰Java2面向对象编程-3继承和多态-2多态
  18. Dell 服务器阵列扩容【经验分享(转)】
  19. The difference between the request time and the current time is too large.阿里云oss上传图片报错
  20. linux上jar包的运行

热门文章

  1. JS数组和函数 小记
  2. C++学习笔记1(Windows程序运行原理及程序编写流程)
  3. 如何把连接字符串放到App.cfg配置文件中
  4. C++/CLI 本地字符串和托管字符串之间的转换
  5. c# 根据父节点id,找到所有的子节点数据
  6. 集群搭建之Hive配置要点
  7. uestc 1072 a ^ b
  8. fuzhou 1683 纪念SlingShot ***
  9. JavaWeb学习总结(四):Servlet开发(二)
  10. js中变量声明有var和没有var的区别