1. 前端路由啟蒙
什么是路由呢拇囊?
答:路由(routing)就是通過互聯(lián)的網(wǎng)絡(luò)把信息從源地址傳輸?shù)侥康牡刂返幕顒?dòng)迂曲。(維基百科)
我們每個(gè)人上網(wǎng)都會(huì)接觸到路由器和光貓(調(diào)制解調(diào)器,虛擬信號(hào)變成數(shù)字信號(hào)寥袭,數(shù)字變成虛擬信號(hào))
簡(jiǎn)單來說如果家中有一個(gè)人以上人上網(wǎng)就會(huì)用到路由器路捧,路由器的作用就是分發(fā)請(qǐng)求
只要是分發(fā)請(qǐng)求都可以叫做路由
那么前端的路由是指什么呢?
我們?cè)跒g覽網(wǎng)頁的時(shí)候經(jīng)常切換頁面传黄,如果每個(gè)頁面都是一個(gè)div杰扫,如何知道用戶要使用那個(gè)頁面呢?那就是用網(wǎng)址
舉個(gè)例子: 有網(wǎng)址
http://127.00.1/#1 代表展示div1
http://127.00.1/#2 代表展示div2
http://127.00.1/#3 代表展示div3
http://127.00.1/#4 代表展示div4
怎么在一個(gè)頁面通過URL的不同切換展示內(nèi)容(這里用div代替)
<div id="app"></div>
<div id="div1" style="display: none">
1
</div>
<div id="div2" style="display: none">
2
</div>
<div id="div3" style="display: none">
3
</div>
<div id="div4" style="display: none">
4
</div>
const number = window.location.hash.substr(1)
// window.location.hash 是包含塊標(biāo)識(shí)符的DOMString膘掰,開頭有一個(gè)“#”
const div = document.querySelector(`#div${number}`)
const app = document.querySelector('#app')
div.style.display = 'block'
app.appendChild(div)
這樣在本地預(yù)覽網(wǎng)址后加入/#1,便會(huì)展示div1章姓,其余同理
我想通過a標(biāo)簽點(diǎn)擊切換顯示內(nèi)容呢
<!-- hashchange event 當(dāng)URL的片段標(biāo)識(shí)符更改時(shí),將觸發(fā)hashchange事件 (跟在#符號(hào)后面的URL部分识埋,包括#符號(hào) -->
<a href="#1">go to 1</a><br>
<a href="#2">go to 2</a><br>
<a href="#3">go to 3</a><br>
<a href="#4">go to 4</a>
<div id="app"></div>
<div id="div1" style="display: none">
1
</div>
<div id="div2" style="display: none">
2
</div>
<div id="div3" style="display: none">
3
</div>
<div id="div4" style="display: none">
4
</div>
let number = window.location.hash.substr(1)
const div = document.querySelector(`#div${number}`)
const app = document.querySelector('#app')
div.style.display = 'block'
app.appendChild(div)
window.addEventListener('hashchange', () => {
number = window.location.hash.substr(1)
const div = document.querySelector(`#div${number}`)
const app = document.querySelector('#app')
app.children[0].style.display = 'none'
document.body.appendChild(app.children[0])
div.style.display = 'block'
app.appendChild(div)
})
這樣便實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的路由跳轉(zhuǎn)凡伊,但是有一個(gè)bug,用戶沒有輸入 #1 的時(shí)候會(huì)把報(bào)錯(cuò)
默認(rèn)路由
let number = window.location.hash.substr(1)
number = number || 1 // 給個(gè)默認(rèn)值 即默認(rèn)路由
但是當(dāng)我輸入的值不是# 1~4 的時(shí)候 又報(bào)錯(cuò)了
保底路由(404路由)
let div2 = document.querySelector(`#div${number2}`)
if (div2) {
div2.style.display = 'block'
} else { // 如果沒有現(xiàn)存的頁面 就顯示404
div2 = document.querySelector('#div404')
div2.style.display = 'block'
}
優(yōu)化一下代碼
function route() {
let number = window.location.hash.substr(1)
let app = document.querySelector('#app')
number = number || 1 // 默認(rèn)路由
// 獲取界面
let div = document.querySelector(`#div${number}`)
// 渲染界面
if (!div) { // 如過輸入正確的hash
div = document.querySelector('#div404')
}
div.style.display = 'block'
// 展示界面
if (app.children.length > 0) {
app.children[0].style.display = 'none'
document.body.appendChild(app.children[0])
}
app.appendChild(div)
}
route() // 初次進(jìn)入窒舟,執(zhí)行路由
// 用戶改變URL中hash事件
window.addEventListener('hashchange', () => {
console.log('hash變了')
route() // 用戶點(diǎn)擊系忙,執(zhí)行路由
})
2. 路由表
上面對(duì)的代碼雖然實(shí)現(xiàn)了最簡(jiǎn)單的路由,但是還是有待改善
let number = window.location.hash.substr(1)
let app = document.querySelector('#app')
number = number || 1 // 默認(rèn)路由
// 獲取界面
let div = document.querySelector(`#div${number}`) // 這里我們hash與div的對(duì)應(yīng)關(guān)系過于單一惠豺,這種對(duì)應(yīng)關(guān)系應(yīng)該是我們可以改變的才更好
我們可以創(chuàng)建一個(gè)路由表
const div1 = document.createElement('div')
div1.innerHTML = '1'
const div2 = document.createElement('div')
div2.innerHTML = '2'
const div3 = document.createElement('div')
div3.innerHTML = '3'
const div4 = document.createElement('div')
div4.innerHTML = '4'
const routeTable = { // 路由表
'1': div1,
'2': div2,
'3': div3,
'4': div4,
}
let div = routeTable[number.toString()] // 根據(jù)hash對(duì)應(yīng)路由
3. hash模式 history模式 memory模式區(qū)別
- hash:上面的實(shí)現(xiàn)便是一種hash模式的路由银还,任何情況下,都可以使用hash模式作為前端的路由耕腾,缺點(diǎn)是SEO不友好见剩,因?yàn)榉?wù)器收不到hash,瀏覽器不會(huì)把#后面的內(nèi)容發(fā)給服務(wù)器的
- history:如果后端能將所有前端路由都渲染到同一個(gè)頁面(不能是404頁面)的時(shí)候就可以使用history模式扫俺,缺點(diǎn)IE8以下不支持
// history 模式
const routeTable = { // 路由表
'/1': div1,
'/2': div2,
'/3': div3,
'/4': div4,
}
let number = window.location.pathname // JS只需該這兩處即可
<a href="/1">go to 1</a><br>
<a href="/2">go to 2</a><br>
<a href="/3">go to 3</a><br>
<a href="/4">go to 4</a> <!-- a標(biāo)簽改成跳轉(zhuǎn)/ -->
但是這樣會(huì)發(fā)現(xiàn)頁面在跳轉(zhuǎn)時(shí)會(huì)刷新苍苞,用戶體驗(yàn)不好
const allA = document.querySelectorAll('.link') //獲取頁面a標(biāo)簽
for (let taga of allA) {
taga.addEventListener('click', (e) => {
e.preventDefault() // 組織默認(rèn)事件
const href = taga.getAttribute('href')
window.history.pushState(null, 'page 2', href)
// 通知
onStateChange(href) // 觸發(fā)事件
})
}
route(app)
function onStateChange() { // 自定義history變化事件
console.log('state變了')
route(app)
}
這樣便可以實(shí)現(xiàn)history模式的路由了
- memory模式:它的特點(diǎn)是頁面信息不在URL后面顯示,適合App頁面的跳轉(zhuǎn)狼纬,reactNative等羹呵,缺點(diǎn)沒有URL不能分享鏈接(單機(jī)版)
let number = window.localStorage.getItem('xxx') //這里使用localStorage存儲(chǔ)跳轉(zhuǎn)信息
const allA = document.querySelectorAll('.link') //獲取頁面a標(biāo)簽
for (let taga of allA) {
taga.addEventListener('click', (e) => {
e.preventDefault()
const href = taga.getAttribute('href')
window.localStorage.setItem('xxx',href)
// 通知
onStateChange(href)
})
}