Vue聲明式導(dǎo)航與編程式導(dǎo)航及導(dǎo)航守衛(wèi)和axios攔截器全面詳細(xì)講解
一、聲明式導(dǎo)航&編程式導(dǎo)航
1. 聲明式導(dǎo)航:以超鏈接方式實(shí)現(xiàn)的頁(yè)面跳轉(zhuǎn),就是聲明式導(dǎo)航
< a href=‘url’> 鏈接文本或圖像 < /a >
< router-link to=‘url’ > 鏈接文本或圖像 < /router-link >
2. 編程式導(dǎo)航:通過(guò)javascript提供的api方法實(shí)現(xiàn)頁(yè)面的跳轉(zhuǎn),就是編程式導(dǎo)航
location.href = ‘url’
location.go(number)
location.replace(‘url’)
3. vue-router中提供的編程式導(dǎo)航的API
? (1)this.$router.push(‘url’):將‘url’添加到路由表中,增加了一條路由記錄
<template>
<div>
<h3>Home 組件</h3>
<button @click="gotoMovie">跳轉(zhuǎn)到 Movie 頁(yè)面</button>
</div>
</template>
<script>
export default {
name: "",
methods:{
gotoMovie(){
this.$router.push('/movie/1')
}
}
}
</script>? (2)this.$router.replace(‘url’):跳轉(zhuǎn)到url中,并替換掉當(dāng)前的歷史記錄
? push 和 replace 的區(qū)別:
? ? 1)push 會(huì)增加一條歷史記錄
? ? 2)replace 不會(huì)增加歷史記錄,而是替換掉當(dāng)前的歷史記錄
? (3)this.$router.go(number):參數(shù)是一個(gè)數(shù)值,頁(yè)面跳轉(zhuǎn)到指定的位置,可以在瀏覽歷史中前進(jìn)和后退。
? ? (4)this.$router.go()的簡(jiǎn)化寫法
? ? ? 1)this.$router.back():在歷史記錄中,后退到上一個(gè)頁(yè)面
? ? ? 2)this.$router.forword():在歷史記錄中,前進(jìn)到下一個(gè)頁(yè)面
<template>
<div>
<h3>Home 組件</h3>
<button @click="goBack">退回上一頁(yè)</button>
</div>
</template>
<script>
export default {
props:['id'],
methods:{
goBack(){
this.$router.go(-1)
}
}
}
</script>二、導(dǎo)航守衛(wèi)
1. 用途:在頁(yè)面導(dǎo)航過(guò)程中實(shí)現(xiàn)重定向、取消路由、權(quán)限驗(yàn)證等業(yè)務(wù)。
? 導(dǎo)航守衛(wèi)分為三類:全局守衛(wèi)、路由獨(dú)享的守衛(wèi)、組件內(nèi)守衛(wèi),可以用于路由導(dǎo)航過(guò)程中的不同階段。
? 每一個(gè)導(dǎo)航守衛(wèi)都有三個(gè)參數(shù):to、from 和 next (router、afterEach 除外)

? 2. 分類:全局守衛(wèi)、組件內(nèi)部守衛(wèi)、路由獨(dú)享的守衛(wèi)
? 3. 全局守衛(wèi)
? ? (1)全局前置守衛(wèi):每次發(fā)生路由的導(dǎo)航跳轉(zhuǎn)時(shí),都會(huì)觸發(fā)全局前置守衛(wèi)。因此,在全局前置守衛(wèi)中,程序員可以對(duì)每個(gè)路由進(jìn)行訪問(wèn)權(quán)限的控制。使用的router.beforeEach(to,from,next){ }來(lái)注冊(cè)。當(dāng)一個(gè)導(dǎo)航觸發(fā)時(shí),全局前置守衛(wèi)按照路由創(chuàng)建的順序調(diào)用。
- to:將要訪問(wèn)的路由的信息對(duì)象
- from:將要離開(kāi)的路由的信息對(duì)象
- ?next:是一個(gè)函數(shù),調(diào)用next()表示當(dāng)前路由已經(jīng)放行
? ? (2)next調(diào)用的情況
? ? 1)用戶擁有了權(quán)限,直接放行:next()
? ? 2)用戶沒(méi)有權(quán)限,強(qiáng)制跳轉(zhuǎn)到指定的頁(yè)面:next(‘/login’)
? ? 3)用戶沒(méi)有權(quán)限,不允許訪問(wèn):next(false)

? ? 4. 全局前置守衛(wèi)的使用
? ? ? (1)創(chuàng)建Login.vue組件
? ? ? (2)在路由文件router / index.js中注冊(cè)全局的前置守衛(wèi)
//App.vue
<template>
<div>
<h2>{{ info }}</h2>
<div>
<label>
賬號(hào):<input type="text" v-model.trim="userName" />
</label>
<br /><br />
<label>
密碼:<input type="password" v-model.trim="passWord" />
</label>
<br /><br />
<button type="button" @click="login">登錄</button>
</div>
</div>
</template>
<script type="text/javascript">
import $http from '../axios/index'
export default {
name: "Login",
data() {
return {
info: '',
userName: '',
passWord: '',
}
},
methods: {
login() {
// //對(duì)登陸信息進(jìn)行驗(yàn)證(實(shí)際中在此處進(jìn)行ajax的請(qǐng)求)
// if('lisi' === this.userName && '1234' === this.passWord){
// sessionStorage.setItem('isAuth',true) //在頁(yè)面緩存中保存isAuth,isAuth=true表示用戶已登錄
// this.info = ''
// //判斷當(dāng)前路由對(duì)象中參數(shù)是否有參數(shù)
// if(this.$route.query.redirect){
// let redirect =this.$route.query.redirect
// this.$router.replace(redirect)//跳轉(zhuǎn)到指定頁(yè)面
// }
// else{
// this.$router.replace('/')//若路由對(duì)象中沒(méi)有redirect,則直接跳轉(zhuǎn)到默認(rèn)的首頁(yè)
// }
// }else{//非法用戶
// sessionStorage.setItem('isAuth',false)
// this.userName = ''
// this.passWord = ''
// this.info = '用戶名或密碼錯(cuò)誤'
// }
$http.post('/test/login', {
userName: this.userName,
passWord: this.passWord
}).then(res => {
if (res.data.code == 200) {
sessionStorage.setItem('auth', res.data.tokenInfo)
this.info = ''
//判斷當(dāng)前路由對(duì)象中參數(shù)是否有參數(shù)
if (this.$route.query.redirect) {
let redirect = this.$route.query.redirect
this.$router.replace(redirect)//跳轉(zhuǎn)到指定頁(yè)面
}
else {
this.$router.replace('/')//若路由對(duì)象中沒(méi)有redirect,則直接跳轉(zhuǎn)到默認(rèn)的首頁(yè)
}
} else {
sessionStorage.setItem('auth', '')
this.userName = ''
this.passWord = ''
this.info = '用戶名或密碼錯(cuò)誤'
}
})
}
}
}
</script>
<style scoped>
</style>//router/insex.js
//注冊(cè)全局前置導(dǎo)航守衛(wèi)
router.beforeEach((to, from, next) => {
//判斷目標(biāo)路由是否是/login,如果是,則直接調(diào)用next()方法
if (to.path == '/login') {
next()
} else {//否則判斷用戶是否已經(jīng)登錄,注意這里是字符串判斷
if (sessionStorage.getItem('auth')!='' && sessionStorage.getItem('auth'!=null)) {
next()
} else {//如果用戶訪問(wèn)的是受保護(hù)的資源,且沒(méi)有登錄,則跳轉(zhuǎn)到登錄頁(yè)面
//并將當(dāng)前路由的完整路徑作為查詢參數(shù)傳遞給Login組件,以便登錄成功后返回先前的頁(yè)面
next({//強(qiáng)制跳轉(zhuǎn)到登錄頁(yè)面
path: '/login',
query:{ redirect:to.fullPath}
}
)
}
}
})三、axios攔截器
1. axios模塊的作用:是對(duì)基于http請(qǐng)求的封裝。在瀏覽器對(duì)異步請(qǐng)求對(duì)象XMLHttpRequest進(jìn)行封裝
2. 攔截器
? (1)請(qǐng)求攔截器:對(duì)客戶端發(fā)起的請(qǐng)求進(jìn)行統(tǒng)一的前期處理(token、時(shí)間戳、cookie等)
? (2)響應(yīng)攔截器:對(duì)服務(wù)器端響應(yīng)給客戶端的數(shù)據(jù)統(tǒng)一進(jìn)行處理之后再發(fā)給客戶端

3. 使用方法
//前端/App.vue
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<br/> <br/>
<button @click="login">登錄</button>
<button @click="test">測(cè)試攔截器</button>
</div>
</template>
<script>
import $http from './axios/index'
export default {
name: 'App',
methods:{
login(){
$http.post('/users/login',{
userName:'lisi',
userPwd:'12345'
}).then(res=>{
if(res.data.code == 200){
sessionStorage.setItem('Auth',res.data.mytoken)
}
}).catch(err=>{
console.log(err);
})
},
test(){
$http.post('/users/test').then(res=>{
console.log(res.data);
}).catch(err=>{
console.log(err);
})
}
}
}
</script>//前端/index.js
import axios from "axios";
//1. 創(chuàng)建axios的實(shí)例,配置基礎(chǔ)路徑
const axiosInstance = axios.create({
baseURL: 'http://localhost:8089',
timeout: 5000
})
//2. 定義請(qǐng)求攔截器:給所有請(qǐng)求都帶上token
axiosInstance.interceptors.request.use((req) => {
let token = sessionStorage.getItem('Auth') //獲取頁(yè)面存儲(chǔ)中的token信息
if (token) { //若token存在
req.headers['Auth'] = token
}
return req;
}, (err) => {
return Promise.reject(err)
})
// 3.響應(yīng)攔截器:對(duì)服務(wù)器響應(yīng)給客戶端的數(shù)據(jù)進(jìn)行統(tǒng)一的處理
axiosInstance.interceptors.response.use((res) => {
//1.對(duì)響應(yīng)數(shù)據(jù)進(jìn)行處理
let result = res.data
let code = result.code
if (code == 200) {
return result
} else {
return Promise.reject(result)
}
}, (err) => {
return Promise.reject(err)
})
export default axiosInstance//后臺(tái)/usrs.js
var express = require('express');
var router = express.Router();
var jwt = require('jsonwebtoken')
/* http://localhost:8089/users/login */
router.post('/login', (req, res)=>{
//1.接收客戶的請(qǐng)求數(shù)據(jù)
let user = {
name:req.body.userName,
pwd:req.body.userPwd
}
//2.定義密鑰
let temp = 'baihualin'
//3.生成token
let token = jwt.sign(user,temp)
res.json({
code:200,
mytoken:token
})
} );
/* http://localhost:8089/users/login */
router.post('/test',(req,res)=>{
//輸出請(qǐng)求頭信息
console.log(req,headers)
let arr = [
{
bookId:1001,
bookName:'Vue2從入門到精通',
publish:'清華大學(xué)出版社'
},
{
bookId:1002,
bookName:'Html從入門到放棄',
publish:'清華大學(xué)出版社'
},
{
bookId:1003,
bookName:'Css都是浮云',
publish:'清華大學(xué)出版社'
},
]
res.json(arr)
})
module.exports = router;到此這篇關(guān)于Vue聲明式導(dǎo)航與編程式導(dǎo)航及導(dǎo)航守衛(wèi)和axios攔截器全面詳細(xì)講解的文章就介紹到這了,更多相關(guān)Vue聲明式導(dǎo)航內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue單頁(yè)面應(yīng)用打開(kāi)新窗口顯示跳轉(zhuǎn)頁(yè)面的實(shí)例
今天小編就為大家分享一篇vue單頁(yè)面應(yīng)用打開(kāi)新窗口顯示跳轉(zhuǎn)頁(yè)面的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
vue使用高德地圖實(shí)現(xiàn)實(shí)時(shí)定位天氣預(yù)報(bào)功能
這篇文章主要介紹了vue使用高德地圖實(shí)現(xiàn)實(shí)時(shí)天氣預(yù)報(bào)功能,根據(jù)定位功能,使用高德地圖實(shí)現(xiàn)定位當(dāng)前城市的天氣預(yù)報(bào)功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
vue3.0+echarts實(shí)現(xiàn)立體柱圖
這篇文章主要為大家詳細(xì)介紹了vue3.0+echarts實(shí)現(xiàn)立體柱圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
vue2.0實(shí)現(xiàn)點(diǎn)擊其他區(qū)域關(guān)閉自定義div功能
這篇文章主要介紹了vue2.0實(shí)現(xiàn)點(diǎn)擊其他區(qū)域關(guān)閉自定義div功能實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06
Vue項(xiàng)目Nginx子目錄部署(Vite和Vue-CLI)
本文主要介紹了Vue項(xiàng)目Nginx子目錄部署(Vite和Vue-CLI),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05

