溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在vue中使用axios實(shí)現(xiàn)一個(gè)登陸功能

發(fā)布時(shí)間:2021-04-09 17:22:35 來源:億速云 閱讀:853 作者:Leah 欄目:web開發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)怎么在vue中使用axios實(shí)現(xiàn)一個(gè)登陸功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識(shí)有一定的了解。

登陸流程為:

1、提交登陸表單,拿到后臺(tái)返回的數(shù)據(jù)

2、將數(shù)據(jù)存入vuex

vuex配置

這里直接跳過安裝之類的,百度一大堆,我直接上代碼

// store index.js
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
// 初始化時(shí)用sessionStore.getItem('token'),這樣子刷新頁面就無需重新登錄
const state = {
 user: window.sessionStorage.getItem('user'),
 token: window.sessionStorage.getItem('token')
}
const mutations = {
 //將token保存到sessionStorage里,token表示登陸狀態(tài)
 SET_TOKEN: (state, data) => {
 state.token = data
 window.sessionStorage.setItem('token', data) 
 },
 //獲取用戶名
 GET_USER: (state, data) => {
 // 把用戶名存起來
 state.user = data
 window.sessionStorage.setItem('user', data)
 },
 //登出
 LOGOUT: (state) => {
 // 登出的時(shí)候要清除token
 state.token = null
 state.user = null
 window.sessionStorage.removeItem('token')
 window.sessionStorage.removeItem('user')
 }
}

const actions = {
}
export default new Vuex.Store({
 state,
 mutations,
 actions
})

1、我在這里是將登錄狀態(tài)token,和用戶名user存在sessionStorage里,以便組件使用,如果token為true則表示用戶已經(jīng)登陸sessionStorage和token這兩個(gè)東西很簡單用法自行百度

2、不要忘了在main.js引入store,vue實(shí)例中也要加入store

main.js

import store from './store/index'

new Vue({
 el: '#app',
 router,
 store,
 components: { App },
 template: '<App/>'
})

vue-route配置

import Vue from 'vue'
import Router from 'vue-router'
import Login from '../components/Login'
import Activity from '../components/Activity'
import Index from '../components/Index'
import store from '../store/index'

Vue.use(Router)

const router = new Router({
 routes: [
 {
  path: '/',
  name: '/',
  component: Index
 },
 {
  path: '/login',
  name: 'login',
  component: Login
 },
 {
  path: '/activity',
  name: 'activity',
  component: Activity,
  meta: {
  requireAuth: true // 添加該字段,表示進(jìn)入這個(gè)路由是需要登錄的
  }
 }
 ]
})

// 注冊全局鉤子用來攔截導(dǎo)航
router.beforeEach((to, from, next) => {
 const token = store.state.token
 if (to.meta.requireAuth) { // 判斷該路由是否需要登錄權(quán)限
 if (token) { // 通過vuex state獲取當(dāng)前的token是否存在
  next()
 } else {
  console.log('該頁面需要登陸')
  next({
  path: '/login'
  // query: {redirect: to.fullPath} // 將跳轉(zhuǎn)的路由path作為參數(shù),登錄成功后跳轉(zhuǎn)到該路由
  })
 }
 } else {
 next()
 }
})

export default router

這里我用到router.beforeEach來實(shí)現(xiàn)攔截登陸,

1、在需要驗(yàn)證的路由的meta里加入我們自己的requireAuth
2、router.beforeEach里通過requireAuth驗(yàn)證該組件是否需要登陸
3、驗(yàn)證token如果為flase就表示未登陸跳轉(zhuǎn)到登錄頁

axios發(fā)送請求

submitLogin () {
 this.$refs.loginForm.validate(valid => {
 if (valid) {
  axios.post('/login', {
  user: this.loginForm.user,
  pass: this.loginForm.pass
  })
  .then((response) => {
   if (response.status === 200) {
   this.$store.commit('SET_TOKEN', response.data.token)
   this.$store.commit('GET_USER', response.data.user)
   this.$message({
    message: '登陸成功',
    type: 'success'
   })
   this.$router.push({name: 'activity'})
   }
  })
  .catch(function (error) {
   console.log(error)
  })
 } else {
  console.log('error submit!!')
  return false
 }
 })
},

后臺(tái)我沒寫,是用mock.js攔截ajax請求

因?yàn)槲矣玫氖莈lement-ui所以上面代碼有一些直接無視,看核心的就行

1、在數(shù)據(jù)返回成功后用this.$store.commit來更新vuex里的數(shù)據(jù)

2、登陸成功后跳轉(zhuǎn)this.$router.push()跳轉(zhuǎn)頁面,

這里注意,如果你在前面導(dǎo)航攔截的鉤子用了query: {redirect: to.fullPath}的話,這里就 用 this.$router.push(this.$route.query.redirect);這樣頁面就能跳到

關(guān)于怎么在vue中使用axios實(shí)現(xiàn)一個(gè)登陸功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI