前端路由两种模式---hash模式、history模式|精讲

时间:2020-08-17 22:38:42   收藏:0   阅读:111

路由

路由指的是通过不同 URL 展示不同页面或者内容的功能

路由的概念来源于服务端,在服务端中路由描述的是 URL 与处理函数之间的映射关系。

在 Web 前端单页应用 SPA(Single Page Application)中,路由描述的是 URL 与 UI 之间的映射关系,这种映射是单向的,即 URL 变化引起 UI 更新(无需刷新页面)。

 

前端路由

 

核心

 

hash模式

 

 

 

 

window.location.hash = ‘qq‘ // 设置 url 的 hash,会在当前url后加上 ‘#qq‘

var hash = window.location.hash // ‘#qq‘  

window.addEventListener(‘hashchange‘, function(){ 
  
})

 

 

history模式

 

 

window.history.pushState(state, title, url) 

window.history.replaceState(state, title, url)

window.addEventListener("popstate", function() {
  
});

 

 

 

原文:https://www.cnblogs.com/isremya/p/13520283.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!