Vue路由模式中的hash和history模式詳細(xì)介紹
1. 路由概念
路由的本質(zhì)就是一種對應(yīng)關(guān)系,根據(jù)不同的URL請求,返回對應(yīng)不同的資源。那么url地址和真實(shí)的資源之間就有一種對應(yīng)的關(guān)系,就是路由。
SPA(Single Page Application)單頁面應(yīng)用程序,基于前端路由而起:整個(gè)網(wǎng)站只有一個(gè)頁面,通過監(jiān)聽地址欄中的變化事件,來通過Ajax局部更新內(nèi)容信息顯示、同時(shí)支持瀏覽器地址欄的前進(jìn)和后退操作。
前端路由有兩種模式:hash 模式和 history 模式。
2. hash模式
概述:
hash 路由模式是這樣的:http://xxx.abc.com/#/xx。 有帶#號,后面就是 hash 值的變化。改變后面的 hash 值,它不會向服務(wù)器發(fā)出請求,因此也就不會刷新頁面。并且每次 hash 值發(fā)生改變的時(shí)候,會觸發(fā) hashchange 事件。因此我們可以通過監(jiān)聽該事件,來知道 hash 值發(fā)生了哪些變化。
window.addEventListener('hashchange', ()=>{
// 通過 location.hash 獲取到最新的 hash 值
console.log(location.hash);
});
使用:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>hash路由</title>
</head>
<body>
<ul>
<li>
<!-- 通過標(biāo)簽導(dǎo)航 聲明式導(dǎo)航 -->
<a href="#/home" rel="external nofollow" >首頁</a>
<!-- location.href='#/home' js方式進(jìn)行導(dǎo)航切換 編程式導(dǎo)航 -->
</li>
<li>
<a href="#/about" rel="external nofollow" >關(guān)于</a>
</li>
</ul>
<div id="routerView"></div>
<script>
const routerRender = () => {
// 每次都置空hash
let html = ''
// 根據(jù)地址欄hash值的不同返回對應(yīng)的資源
try {
// 如果hash值為空就給一個(gè)home
let hash = location.hash || '#/home'
html = component[hash.slice(2)]()
} catch (error) {
html = `<div>404</div>`
}
// 渲染到頁面上
document.getElementById('routerView').innerHTML = html
}
const component = {
home() {
return `<div>home頁面</div>`
},
about() {
return '<div>關(guān)于頁面</div>'
}
}
window.onload = function () {
routerRender()
}
// 事件,監(jiān)聽地址欄中的hash值變化,實(shí)現(xiàn)回退
window.addEventListener('hashchange', routerRender)
</script>
</body>
</html>
注意:hash 模式既可以通過聲明式導(dǎo)航,也可以通過編程式導(dǎo)航,上面的案例展示的是聲明式導(dǎo)航。而下面將要講到的 history 模式只能通過編程式導(dǎo)航實(shí)現(xiàn),因?yàn)?history 是 js 對象。
優(yōu)缺點(diǎn):
優(yōu)點(diǎn):hash模式兼容性很強(qiáng),刷新瀏覽器,頁面還會存在
缺點(diǎn):地址欄不優(yōu)雅,有#存在,不利于seo,記憶困難
3. history路由模式
概述:
HTML5的History API為瀏覽器的全局history對象增加了該擴(kuò)展方法。它是一個(gè)瀏覽器(bom)的一個(gè)接口,在window對象中提供了onpopstate事件來監(jiān)聽歷史棧的改變,只要?dú)v史棧有信息發(fā)生改變的話,就會觸發(fā)該事件。
history.pushState({},title,url); // 向歷史記錄中追加一條記錄
history.replaceState({},title,url); // 替換當(dāng)前頁在歷史記錄中的信息。
window.addEventListener('popstate', function(event) {
console.log(event)
})
使用:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>history模式</title>
</head>
<body>
<ul>
<li>
<a href="/home" rel="external nofollow" >首頁</a>
</li>
<li>
<a href="/about" rel="external nofollow" >關(guān)于</a>
</li>
</ul>
<div id="routerView"></div>
<script>
const component = {
home() {
return `<div>home頁面</div>`
},
about() {
return '<div>關(guān)于頁面</div>'
}
}
const routerRender = pathname => {
let html = ''
try {
html = component[pathname]()
} catch (error) {
html = `<div>404</div>`
}
document.getElementById('routerView').innerHTML = html
}
// history模式,它的路由導(dǎo)航,只能通過js來完成 , history它是js對象
// 給鏈接添加點(diǎn)擊事件
document.querySelectorAll('a').forEach(node => {
node.addEventListener('click', function (evt) {
// 阻止a標(biāo)簽的默認(rèn)跳轉(zhuǎn)行為
evt.preventDefault()
// 跳轉(zhuǎn)到指定的地址,能回退
// history.pushState
// 跳轉(zhuǎn)到指定持址,不能回退
// history.replaceState
history.pushState({}, null, this.href)
// 渲染
routerRender(this.href.match(/\/(\w+)$/)[1])
})
})
// 在網(wǎng)頁加載完畢后立刻執(zhí)行的操作,即當(dāng) HTML 文檔加載完畢后,立刻渲染 home 中的標(biāo)簽
window.onload = () => {
routerRender('home')
}
// 回退
window.addEventListener('popstate', function () {
routerRender(location.pathname.slice(1))
})
</script>
</body>
</html>
優(yōu)缺點(diǎn):
缺點(diǎn):history模式,兼容性較差,刷新頁面,頁面會404,需要服務(wù)器端配置支持
優(yōu)點(diǎn):地址欄更優(yōu)雅,方便記憶,有利于有seo
到此這篇關(guān)于Vue路由模式中的hash和history模式詳細(xì)介紹的文章就介紹到這了,更多相關(guān)Vue路由模式 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解vue使用vue-layer-mobile組件實(shí)現(xiàn)toast,loading效果
這篇文章主要介紹了詳解vue使用vue-layer-mobile組件實(shí)現(xiàn)toast,loading效果,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08
vue + webpack如何繞過QQ音樂接口對host的驗(yàn)證詳解
這篇文章主要給大家介紹了關(guān)于利用vue + webpack如何繞過QQ音樂接口對host的驗(yàn)證的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
Vue3封裝ElImageViewer預(yù)覽圖片的示例代碼
本文主要介紹了Vue3封裝ElImageViewer預(yù)覽圖片的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
vue2中前端實(shí)現(xiàn)語音播報(bào)的詳細(xì)過程
vue中語音播報(bào),目前本人寫的過程中,遇到了兩種情況,第一種是后端直接返回一個(gè)mp3的播放url,第二種就是播報(bào)的內(nèi)容需要前端自己拼接的,關(guān)于兩種方法,我都說一下如何實(shí)現(xiàn),感興趣的朋友一起看看吧2024-07-07

