Vue-Element-Admin集成自己的接口實(shí)現(xiàn)登錄跳轉(zhuǎn)
1、先看一下請求配置文件,看axios.create這個方法,baseURL是基礎(chǔ)路由
baseURL:process.env.VUE_APP_BASE_API,
路徑:src-utils-request.js
2、然后再看service.interceptors.request.use,設(shè)置token請求頭,我后端集成的是jwt,所以請求頭是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這行注釋掉,這個用來模擬數(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的登錄接口,添加如下代碼,如圖,把官方的請求方式注釋掉,this.loginForm是請求參數(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做緩存,不然登錄頁跳轉(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 } //請求服務(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)楂@取用戶信息接口沒寫,所以數(shù)據(jù)直接寫死,根據(jù)自己的做調(diào)整
getInfo({ commit, state }) { return new Promise((resolve, reject) => { /** * 這里請求用戶信息和權(quán)限,目前接口沒做,只注釋了,data寫死 * */ // 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,可以登錄到首頁了
到此這篇關(guān)于Vue-Element-Admin集成自己的接口實(shí)現(xiàn)登錄跳轉(zhuǎn)的文章就介紹到這了,更多相關(guān)Vue-Element-Admin登錄跳轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue?+?element-ui?背景圖片設(shè)置方式
這篇文章主要介紹了Vue?+?element-ui?背景圖片設(shè)置方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08Vue讀取本地靜態(tài).md并側(cè)邊欄導(dǎo)航跳轉(zhuǎn)、展示.md文件的操作方法
這篇文章主要介紹了Vue讀取本地靜態(tài).md并側(cè)邊欄導(dǎo)航跳轉(zhuǎn)、展示.md文件,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08Slots Emit和Props穿透組件封裝實(shí)現(xiàn)摸魚加鐘
這篇文章主要為大家介紹了Slots Emit和Props穿透組件封裝實(shí)現(xiàn)示例詳解,為摸魚加個鐘,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Vue實(shí)現(xiàn)遠(yuǎn)程獲取路由與頁面刷新導(dǎo)致404錯誤的解決
這篇文章主要介紹了Vue實(shí)現(xiàn)遠(yuǎn)程獲取路由與頁面刷新導(dǎo)致404錯誤的解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01Vue使用axios發(fā)送請求并實(shí)現(xiàn)簡單封裝的示例詳解
這篇文章主要介紹了Vue使用axios發(fā)送請求并實(shí)現(xiàn)簡單封裝,主要包括安裝axios及簡單使用配置方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06vue中{__ob__: observer}對象轉(zhuǎn)化為數(shù)組進(jìn)行遍歷方式
這篇文章主要介紹了vue中{__ob__: observer}對象轉(zhuǎn)化為數(shù)組進(jìn)行遍歷方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10