- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > web開(kāi)發(fā) > JavaScript >
- Vue-Element-Admin集成自己的接口實(shí)現登錄跳轉
1、先看一下請求配置文件,看axios.create這個(gè)方法,baseURL是基礎路由
baseURL:process.env.VUE_APP_BASE_API,
路徑:src-utils-request.js
2、然后再看service.interceptors.request.use,設置token請求頭,我后端集成的是jwt,所以請求頭是Authentication,如圖
config.headers['Authentication'] = getToken()
3.設置自己的狀態(tài)碼,看service.interceptors.response.use,如圖,設置為自己的狀態(tài)碼
{ "code": 1, "data": { "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJBY2NvdW50SWQiOiJhZG1pbiIsIm5iZiI6MTYyNDE3NTM4MiwiZXhwIjoxNjI0MTc1NDQyLCJpYXQiOjE2MjQxNzUzODJ9.7p8EHMx1b4-yIMRN7Qxden3nZsDmBvevHEf-3oVhFMg", "message": "登錄成功", "state": true } }
4、改.env.production和.env.development里面的api都為空,圖只展示.env.production
5、改基礎路由配置,在devServer后面添加如下代碼(before這行注釋掉,這個(gè)用來(lái)模擬數據的,用不到),如圖
// 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]: '' } } }
到這里基礎路由基本配置好了
6、在src-view-login-index.html文件中,找到Vue-Element-Admin的登錄接口,添加如下代碼,如圖,把官方的請求方式注釋掉,this.loginForm是請求參數
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、設置用戶(hù)登錄成功后的跳轉,登錄后必須把token做緩存,不然登錄頁(yè)跳轉不了
在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ù)器需要的登錄參數 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方法,修改代碼如下,因為獲取用戶(hù)信息接口沒(méi)寫(xiě),所以數據直接寫(xiě)死,根據自己的做調整
getInfo({ commit, state }) { return new Promise((resolve, reject) => { /** * 這里請求用戶(hù)信息和權限,目前接口沒(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í)現登錄跳轉的文章就介紹到這了,更多相關(guān)Vue-Element-Admin登錄跳轉內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站