Vue-Element-Admin集成自己的接口實(shí)現(xiàn)登錄跳轉(zhuǎn)
1、先看一下請(qǐng)求配置文件,看axios.create這個(gè)方法,baseURL是基礎(chǔ)路由
baseURL:process.env.VUE_APP_BASE_API,
路徑:src-utils-request.js

2、然后再看service.interceptors.request.use,設(shè)置token請(qǐng)求頭,我后端集成的是jwt,所以請(qǐng)求頭是Authentication,如圖
config.headers['Authentication'] = getToken()

3.設(shè)置自己的狀態(tài)碼,看service.interceptors.response.use,如圖,設(shè)置為自己的狀態(tài)碼

這是我服務(wù)器響應(yīng)的數(shù)據(jù),如下,1是正常響應(yīng)數(shù)據(jù)
{
"code": 1,
"data": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJBY2NvdW50SWQiOiJhZG1pbiIsIm5iZiI6MTYyNDE3NTM4MiwiZXhwIjoxNjI0MTc1NDQyLCJpYXQiOjE2MjQxNzUzODJ9.7p8EHMx1b4-yIMRN7Qxden3nZsDmBvevHEf-3oVhFMg",
"message": "登錄成功",
"state": true
}
}
4、改.env.production和.env.development里面的api都為空,圖只展示.env.production

5、改基礎(chǔ)路由配置,在devServer后面添加如下代碼(before這行注釋掉,這個(gè)用來(lái)模擬數(shù)據(jù)的,用不到),如圖
// before: require('./mock/mock-server.js')
proxy: {
[process.env.VUE_APP_BASE_API]: {
target: 'https://xiaoxingbobo.top',
// target: 'http://192.168.1.119:8081',
// target: 'http://192.168.1.253:8081',
changeOrigin: true,
pathRewrite: {
['^' + process.env.VUE_APP_BASE_API]: ''
}
}
}

到這里基礎(chǔ)路由基本配置好了
6、在src-view-login-index.html文件中,找到Vue-Element-Admin的登錄接口,添加如下代碼,如圖,把官方的請(qǐng)求方式注釋掉,this.loginForm是請(qǐng)求參數(shù)
this.loading = true
this.$store.dispatch('user/login', this.loginForm)
.then(() => {
this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
this.loading = false
})
.catch((e) => {
this.tool.log(e)
this.loading = false
})

7、設(shè)置用戶登錄成功后的跳轉(zhuǎn),登錄后必須把token做緩存,不然登錄頁(yè)跳轉(zhuǎn)不了
在src-store-moduls-use.js,如圖

找到action下的login方法,修改代碼如下
const actions = {
// user login
login({
commit
}, userInfo) {
const {
accountId,
password
} = userInfo
return new Promise((resolve, reject) => {
console.log('userInfo', userInfo)
//服務(wù)器需要的登錄參數(shù)
const payload = {
accountId: accountId,
password: password
}
//請(qǐng)求服務(wù)器
user.login(payload).then(response => {
const {
data
} = response
console.log('response', response)
commit('SET_TOKEN', data.token)
setToken(data.token)
resolve()
}).catch(error => {
reject(error)
})
})
},
找到getInfo方法,修改代碼如下,因?yàn)楂@取用戶信息接口沒(méi)寫(xiě),所以數(shù)據(jù)直接寫(xiě)死,根據(jù)自己的做調(diào)整
getInfo({
commit,
state
}) {
return new Promise((resolve, reject) => {
/**
* 這里請(qǐng)求用戶信息和權(quán)限,目前接口沒(méi)做,只注釋了,data寫(xiě)死
* */
// user.getInfo(state.token).then(response => {
// const {
// data
// } = response
const {
data
} = {
data: {
roles: ['admin'],
introduction: 'Administrator',
avatar: 'https://cloud.xiaoxingbobo.top/nongzhibang/20210429/1107491622257669573',
name: 'administrator'
}
}
if (!data) {
reject('Verification failed, please Login again.')
}
const {
roles,
name,
avatar,
introduction,
token
} = data
// roles must be a non-empty array
if (!roles || roles.length <= 0) {
reject('getInfo: roles must be a non-null array!')
}
commit('SET_ROLES', roles)
commit('SET_NAME', name)
commit('SET_AVATAR', avatar)
commit('SET_INTRODUCTION', introduction)
commit('SET_TOKEN', token)
resolve(data)
// }).catch(error => {
// reject(error)
// })
})
},
這樣就搞定了Vue-Element-Admin,可以登錄到首頁(yè)了

到此這篇關(guān)于Vue-Element-Admin集成自己的接口實(shí)現(xiàn)登錄跳轉(zhuǎn)的文章就介紹到這了,更多相關(guān)Vue-Element-Admin登錄跳轉(zhuǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Avue實(shí)現(xiàn)動(dòng)態(tài)查詢與數(shù)據(jù)展示的示例代碼
Avue是一個(gè)基于Vue.js的前端框架,它是由阿里云開(kāi)發(fā)的一款企業(yè)級(jí)UI組件庫(kù),旨在提供一套全面、易用且高性能的界面解決方案本文介紹了Avue實(shí)現(xiàn)動(dòng)態(tài)查詢與數(shù)據(jù)展示的示例,需要的朋友可以參考下2024-08-08
Vue中使用Printjs插件實(shí)現(xiàn)打印功能
Print.js 主要是為了幫助我們直接在我們的應(yīng)用程序中打印 PDF 文件,無(wú)需離開(kāi)界面,也無(wú)需使用嵌入,這篇文章主要介紹了Vue中使用Printjs插件實(shí)現(xiàn)打印功能,需要的朋友可以參考下2022-08-08
Vue3利用vue-plugin-hiprint插件實(shí)現(xiàn)無(wú)預(yù)覽打印功能
在MES管理系統(tǒng)中需要實(shí)現(xiàn)條碼數(shù)據(jù)從接口返回后,直接調(diào)用打印機(jī)進(jìn)行打印,跳過(guò)瀏覽器的預(yù)覽確定那一步,在嘗試很多JS的方式和插件后,都無(wú)法實(shí)現(xiàn)該功能,所以本文介紹了Vue3如何利用vue-plugin-hiprint插件實(shí)現(xiàn)無(wú)預(yù)覽打印功能,需要的朋友可以參考下2025-04-04

