前端路由
hash 模式
https://www.baidu.com#index
location.hash = #index
hash
特性:
URL
中hash
值只是客户端的一种状态,当向服务器端发出请求时,hash
部分不会被发送。hash
值的改变,会在浏览器的访问历史中增加一个记录,能通过浏览器的回退、前进按钮控制hash
的切换。hashchange
事件监听hash
的变化。
history模式
history
特性:
pushState
和repalceState
的标题(title
):一般浏览器会忽略 ;- 使用
popstate
事件来监听url
的变化; history.pushState()
或history.replaceState()
不会触发popstate
事件