vue-router
vue-router是根据hash history两种方式实现前端路由的,更新视图不请求页面是前端路由原理的核心之一,
一,目前在浏览器环境中这一功能的实现主要有两种方式
1.hash ---- 利用URL中的hash(“#”)
2.利用History interface在 HTML5中新增的方法,
默认hash
history 注:如果浏览器不支持history新特性,则采用hash方式
如果不在浏览器环境则使用abstract(node环境下)
mode:‘hash’ ,多了 “ # ”
http://localhost:8080/#/recommend
mode:‘history’
http://localhost:8080/recommend
hashhistory
两个方法:HashHistory.push() 和 HashHistory.replace()
HTML5history
两个方法:pushstatre() replacestate()
History interface是浏览器历史记录栈提供的接口,通过back(), forward(), go()等方法,我们可以读取浏览器历史记录栈的信息,进行各种跳转操作。
从HTML5开始,History interface有进一步修炼:pushState(), replaceState() 这下不仅是读取了,还可以对浏览器历史记录栈进行修改1.push
与hash模式类似,只是将window.hash改为history.pushState
2.replace
与hash模式类似,只是将window.replace改为history.replaceState
3.监听地址变化
在HTML5History的构造函数中监听popState(window.onpopstate)