vue實(shí)現(xiàn)的請(qǐng)求服務(wù)器端API接口示例
本文實(shí)例講述了vue實(shí)現(xiàn)的請(qǐng)求服務(wù)器端API接口。分享給大家供大家參考,具體如下:
import axios from 'axios'
import router from '@/router'
axios.defaults.timeout = 3000
axios.defaults.baseURL = ''
axios.interceptors.request.use(
config => {
// const token = getCookie('名稱')
config.data = config.data
config.headers = {
'Content-Type': 'application/json; charset=utf-8'
}
if (config.url === '/api/login/index') {
} else {
if (localStorage.getItem('Authorization')) {
config.headers.Authorizatior = localStorage.getItem('Authorization')
}
}
// if (token) {
// config.params = {'token': token}
// }
return config
},
error => {
return Promise.reject(error)
}
)
axios.interceptors.response.use(
response => {
//返回錯(cuò)誤跳轉(zhuǎn)到首頁
if (response.data.code === 0) {
router.push({
path: '/',
querry: {
redirect: router.currentRoute.fullPath
}
})
}
return response
},
error => {
if (error.response) {
switch (error.response.status) {
case 401:
localStorage.removeItem('Authorization')
router.push('/login')
}
}
return Promise.reject(error)
}
)
/**
* 封裝get方法
* @param url
* @param data
* @returns {Promise}
*/
export function fetch (url, params = {}) {
return new Promise((resolve, reject) => {
axios.get(url, {
params: params
})
.then(response => {
resolve(response.data)
})
.catch(err => {
reject(err)
})
})
}
/**
* 封裝post請(qǐng)求
* @param url
* @param data
* @returns {Promise}
*/
export function post (url, data = {}) {
return new Promise((resolve, reject) => {
axios.post(url, data)
.then(response => {
resolve(response.data)
}, err => {
reject(err)
})
})
}
/**
* 封裝patch請(qǐng)求
* @param url
* @param data
* @returns {Promise}
*/
export function patch (url, data = {}) {
return new Promise((resolve, reject) => {
axios.patch(url, data)
.then(response => {
resolve(response.data)
}, err => {
reject(err)
})
})
}
/**
* 封裝put請(qǐng)求
* @param url
* @param data
* @returns {Promise}
*/
export function put (url, data = {}) {
return new Promise((resolve, reject) => {
axios.put(url, data)
.then(response => {
resolve(response.data)
}, err => {
reject(err)
})
})
}
main.js調(diào)用
import { fetch, post, patch, put } from '@/util/fetch'
Vue.prototype.get = fetch
Vue.prototype.post = post
Vue.prototype.patch = patch
Vue.prototype.put = put
視圖頁面使用
export default {
name: 'login',
data () {
return {
mobile: '',
password: ''
}
},
components: {
XInput,
XButton,
Group,
Box
},
methods: {
handleLogin () {
let params = {}
params.username = this.mobile
params.password = this.password
this.post('/api/driver/index', params).then((data) => {
console.log(data)
}).catch((error) => {
console.log(error)
})
}
}
}
希望本文所述對(duì)大家vue.js程序設(shè)計(jì)有所幫助。
相關(guān)文章
vue-router(this.$router)如何在新窗口打開路由跳轉(zhuǎn)頁面
這篇文章主要介紹了vue-router(this.$router)如何在新窗口打開路由跳轉(zhuǎn)頁面問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
vue3+ts中ref與reactive指定類型實(shí)現(xiàn)示例
這篇文章主要為大家介紹了vue3+ts中ref及reactive如何指定類型的實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
解決Vue3使用Element-Plus導(dǎo)航刷新后active高亮消失的問題
這篇文章主要給大家介紹了如何解決Vue3使用Element-Plus導(dǎo)航刷新后active高亮消失的問題,文中有相關(guān)的代碼講解,需要的朋友可以參考下2023-08-08
通過實(shí)例解析vuejs如何實(shí)現(xiàn)調(diào)試代碼
這篇文章主要介紹了通過實(shí)例解析vuejs如何實(shí)現(xiàn)調(diào)試代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
vue中input標(biāo)簽上傳本地文件或圖片后獲取完整路徑的解決方法
本文給大家介紹vue中input標(biāo)簽上傳本地文件或圖片后獲取完整路徑,如E:\medicineOfCH\stageImage\xxx.jpg,本文給大家分享完美解決方案,感興趣的朋友跟隨小編一起看看吧2023-04-04
element ui 表格動(dòng)態(tài)列顯示空白bug 修復(fù)方法
今天小編就為大家分享一篇element ui 表格動(dòng)態(tài)列顯示空白bug 修復(fù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09

