vue+axios新手實踐實現(xiàn)登陸的示例代碼
其實像這類的文章網(wǎng)上已經(jīng)有很多很好的,寫這篇文章,相當于是做個筆記,以防以后忘記
用到的:1、 vuex 2、axios 3、vue-route
登陸流程為:
1、提交登陸表單,拿到后臺返回的數(shù)據(jù)
2、將數(shù)據(jù)存入vuex
vuex配置
這里直接跳過安裝之類的,百度一大堆,我直接上代碼
// store index.js import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) // 初始化時用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) => { // 登出的時候要清除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這兩個東西很簡單用法自行百度
2、不要忘了在main.js引入store,vue實例中也要加入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 // 添加該字段,表示進入這個路由是需要登錄的 } } ] }) // 注冊全局鉤子用來攔截導(dǎo)航 router.beforeEach((to, from, next) => { const token = store.state.token if (to.meta.requireAuth) { // 判斷該路由是否需要登錄權(quán)限 if (token) { // 通過vuex state獲取當前的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來實現(xiàn)攔截登陸,
1、在需要驗證的路由的meta里加入我們自己的requireAuth
2、router.beforeEach里通過requireAuth驗證該組件是否需要登陸
3、驗證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 } }) },
后臺我沒寫,是用mock.js攔截ajax請求
因為我用的是element-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);這樣頁面就能跳到
你跳到登陸頁面前要去的那個路由了
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue實現(xiàn)登陸登出的實現(xiàn)示例
- Vue2.0 axios前后端登陸攔截器(實例講解)
- 詳解用vue.js和laravel實現(xiàn)微信授權(quán)登陸
- 基于vue-cli3和element實現(xiàn)登陸頁面
- Vue 前端實現(xiàn)登陸攔截及axios 攔截器的使用
- 詳解vue2.0+axios+mock+axios-mock+adapter實現(xiàn)登陸
- 詳解springboot和vue前后端分離開發(fā)跨域登陸問題
- Vue 頁面權(quán)限控制和登陸驗證功能的實例代碼
- vue+koa2實現(xiàn)session、token登陸狀態(tài)驗證的示例
- vue實現(xiàn)登陸功能
相關(guān)文章
vue?cli?局部混入mixin和全局混入mixin的過程
這篇文章主要介紹了vue?cli?局部混入mixin和全局混入mixin的過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05解決vue無法加載文件C:\Users\Administrator\AppData\Roaming\npm\vue.ps
這篇文章主要介紹了解決vue無法加載文件C:\Users\Administrator\AppData\Roaming\npm\vue.ps1因為在此系統(tǒng)上禁止運行腳本問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03