vuex實現登入狀態的儲存,未登入狀態不允許瀏覽的方法,vuex未登入

來源:互聯網
上載者:User

vuex實現登入狀態的儲存,未登入狀態不允許瀏覽的方法,vuex未登入

基礎思路就是使用vuex狀態管理來儲存登入狀態(其實就是存一個值,例如token),然後在路由跳轉前進行登入狀態的判斷,可以使用vue-router的全域前置守衛beforeEach,也可以使用路由獨享的守衛beforeEnter。

導航守衛

正如其名,vue-router``` 提供的導航守衛主要用來通過跳轉或取消的方式守衛導航。有多種機會植入路由導航過程中:全域的, 單個路由獨享的, 或者組件級的。 記住參數或查詢的改變並不會觸發進入/離開的導航守衛。你可以通過觀察 $route 對象來應對這些變化,或使用beforeRouteUpdate的組件內守衛。

完整的導航解析流程

1、導航被觸發。

2、在失活的組件裡調用離開守衛。

3、調用全域的 beforeEach 守衛。

4、在重用的組件裡調用 beforeRouteUpdate 守衛 (2.2+)。

5、在路由配置裡調用 beforeEnter。

6、解析非同步路由群組件。

7、在被啟用的組件裡調用 beforeRouteEnter。

8、調用全域的 beforeResolve 守衛 (2.5+)。

9、導航被確認。

10、調用全域的 afterEach 鉤子。

11、觸發 DOM 更新。

12、用建立好的執行個體調用 beforeRouteEnter 守衛中傳給 next 的回呼函數。

全域守衛

你可以使用 router.beforeEach註冊一個全域前置守衛

const router = new VueRouter({ ... })router.beforeEach((to, from, next) => { // ...})

當一個導航觸發時,全域前置守衛按照建立順序調用。守衛是非同步解析執行,此時導航在所有守衛 resolve 完之前一直處於 等待中。

每個守衛方法接收三個參數:

to: Route:即將要進入的目標 路由對象

from: Route:當前置航正要離開的路由

next: Function:一定要調用該方法來 resolve 這個鉤子。執行效果依賴 next 方法的調用參數。

next():進行管道中的下一個鉤子。如果全部鉤子執行完了,則導航的狀態就是 confirmed (確認的)。

next(false):中斷當前的導航。如果瀏覽器的 URL 改變了(可能是使用者手動或者瀏覽器後退按鈕),那麼 URL 地址會重設到 from 路由對應的地址。

next('/')或者next({ path: '/' }): 跳轉到一個不同的地址。當前的導航被中斷,然後進行一個新的導航。

next(error):(2.4.0+) 如果傳入 next 的參數是一個 Error 執行個體,則導航會被終止且該錯誤會被傳遞給 router.onError()註冊過的回調。

確保要調用 next 方法,否則鉤子就不會被 resolved。

路由獨享的守衛

你可以在路由配置上直接定義beforeEnter守衛:

const router = new VueRouter({ routes: [  {   path: '/foo',   component: Foo,   beforeEnter: (to, from, next) => {    // ...   }  } ]})

還有其他部分守衛,詳情可以看官方文檔https://router.vuejs.org/zh-cn/advanced/navigation-guards.html

安裝vuex後

建立store.js

import Vue from 'vue'import Vuex from 'vuex'Vue.use(Vuex);const state = {  isLogin: 0}const mutations = {  changeLogin(state,status){    state.isLogin = status;  }}const actions = {  loginAction({commit}){    commit('changeLogin',1);  }}export default new Vuex.Store({  state,  actions,  mutations})

login.vue中

引入import { mapActions,mapState } from 'vuex'

接著進行登入狀態的改變,base_url就是路徑

export default {    name: 'Login',    data(){      return{        loginForm: {          username: '',          password: '',        },        rules: {          username: [            { required: true, message: '請輸入使用者名稱', trigger: 'blur' },          ],          password: [            { required: true, message: '請輸入密碼', trigger: 'blur' }          ],        },        showLogin: false      }    },    mounted(){      this.showLogin = true;    },    computed: {      ...mapState(['isLogin'])    },    methods: {      ...mapActions(['loginAction']),      submitForm(formName){        this.$refs[formName].validate((valid) => {          if(valid){            if(this.loginForm.username == 'aaa' && this.loginForm.password == '111'){              console.log('驗證通過');              this.loginAction();              this.$router.push('manage');            }else{              console.log('帳號密碼出錯');              // this.$message.error('帳號密碼出錯');              this.$message({                type: 'error',                message: '帳號密碼出錯'              });            }            console.log('請求地址: ' + base_url);          }else{            console.log('驗證失敗');            return false;          }        })      }    }  }

接下去只要使用路由守衛即可

beforeEach使用執行個體

router.beforeEach((to,from,next)=>{  if(to.meta.check){    var check = async function(){      const result = await checkUser();      if(result.status == 0){        next();      }else{        alert('使用者未登入');        next({path: '/login'});      }    }    check(); //後台驗證session  }else{    next();  }})

beforeEnter使用執行個體

export default new Router({  routes: [    {     path: '/login',     component: Login    },    {      path: '/manage',      name: '',      component: Manage,      beforeEnter: (to,from,next)=> {  //導航守衛      console.log(to)      console.log(from)      if(store.state.isLogin == 1){       console.log('使用者已經登入');       next();      }else{       console.log('使用者未登入');       next({path: '/login',query:{ Rurl: to.fullPath}}); //未登入則跳轉到登陸介面,query:{ Rurl: to.fullPath}表示把當前路由資訊傳遞過去方便登入後跳回來     }   }     }   ]})

以上這篇vuex實現登入狀態的儲存,未登入狀態不允許瀏覽的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.