Vue之beforeEach非登錄不能訪問的實(shí)現(xiàn)(代碼親測)
后臺的php請求就是接收這兩個參數(shù)
login.vue
<template>
<div class=''>
<input type="text" v-model="name" />
<input type="password" v-model="password" />
<button type="primary" @click="submitForm"><router-link :to="{path:'/'}">提交</router-link></button>
</div>
</template>
<script>
import axios from 'axios';
export default {
data(){
return{
name:'',
password:'',
}
},
methods:{
submitForm:function(){
var params = new URLSearchParams();
params.append('name', this.name);
params.append('password',this.password);
axios({
method: 'post',
url: '/api/bbb.php',
data:params
}).then((resp)=>{
sessionStorage.setItem('token',resp.status)// localStorage
//以key value的形式將值存放到sessionStorage中。
console.log(resp);
}).catch((error)=>{
console.log(error);
})
}
}
}
</script>
router
{
path: '/',
name: 'HelloWorld',
component: HelloWorld,
meta:{requireAuth:true}
},
main.js
router.beforeEach((to, from, next) => {
console.log(to);
console.log(from);
console.log( sessionStorage.getItem('token'))
if (to.meta.requireAuth) { // 判斷該路由是否需要登錄權(quán)限
if(sessionStorage.getItem('token')){ //判斷sessionStorage是否存在token
alert("已經(jīng)登錄了")
next();
}else{
//防止死循環(huán)
alert("暫時未登錄")
if (to.path === '/login') {
next();
return
}else{
next({
path: '/login',
});
}
}
}
else {
next();
}
/*如果本地 存在 token 則 不允許直接跳轉(zhuǎn)到 登錄頁面*/
if(to.fullPath == "/login"){
if(localStorage.getItem('token')){
next({
path:from.fullPath
});
}else {
next();
}
}
});
注意一定要在router實(shí)例前使用
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue router導(dǎo)航守衛(wèi)(router.beforeEach())的使用詳解
- vue-router beforeEach跳轉(zhuǎn)路由驗(yàn)證用戶登錄狀態(tài)
- Vuerouter的beforeEach與afterEach鉤子函數(shù)的區(qū)別
- Vue路由鉤子之a(chǎn)fterEach beforeEach的區(qū)別詳解
- 使用vue-route 的 beforeEach 實(shí)現(xiàn)導(dǎo)航守衛(wèi)(路由跳轉(zhuǎn)前驗(yàn)證登錄)功能
- 關(guān)于vue-router的beforeEach無限循環(huán)的問題解決
相關(guān)文章
vue3?v-bind="$attrs"繼承組件全部屬性的解決方案
這篇文章主要介紹了vue3?v-bind=“$attrs“?繼承組件全部屬性的解決方案,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06
SyntaxError:?/xx.vue:?Unexpected?token,?expected?“,“錯誤解
這篇文章主要為大家介紹了SyntaxError:?/xx.vue:?Unexpected?token,?expected?“,“錯誤解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
vue.js出現(xiàn)Vue.js?not?detected錯誤的解決方案
這篇文章主要介紹了vue.js出現(xiàn)Vue.js?not?detected錯誤的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
Vue項(xiàng)目如何部署到SpringBoot工程下
這篇文章主要介紹了Vue項(xiàng)目如何部署到SpringBoot工程下問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
VUE 直接通過JS 修改html對象的值導(dǎo)致沒有更新到數(shù)據(jù)中解決方法分析
這篇文章主要介紹了VUE 直接通過JS 修改html對象的值導(dǎo)致沒有更新到數(shù)據(jù)中解決方法,結(jié)合實(shí)例形式詳細(xì)分析了VUE使用JS修改html對象的值導(dǎo)致沒有更新到數(shù)據(jù)的原因與解決方法,需要的朋友可以參考下2019-12-12
在vue中使用css modules替代scroped的方法
本篇文章主要介紹了在vue中使用css modules替代scroped的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
Vite+TS+Vue開啟eslint和prettier規(guī)范及校驗(yàn)方式
這篇文章主要介紹了Vite+TS+Vue開啟eslint和prettier規(guī)范及校驗(yàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
Vue3.0插件執(zhí)行原理與實(shí)戰(zhàn)
這篇文章主要介紹了Vue3.0插件執(zhí)行原理與實(shí)戰(zhàn),Vue項(xiàng)目能夠使用很多插件來豐富自己的功能Vue-Router、Vuex等,節(jié)省了我們大量的人力和物力,下面我們就一起來了解Vue3.0插件的原理吧,需要的小伙伴可以參考一下2022-02-02

