寫項目時報下列錯誤,找了半天,結(jié)果才發(fā)現(xiàn)自己在request.js中少寫了一行代碼
一定不要少些代碼文章來源:http://www.zghlxwxcb.cn/news/detail-836076.html
export default request
request.js完整代碼文章來源地址http://www.zghlxwxcb.cn/news/detail-836076.html
import axios from 'axios';
//創(chuàng)建一個新的axios對象
const request = axios.create({
baseURL:'http://localhost:9988',
timeout:30000
})
//request攔截器
//可以自請求發(fā)送前對請求做一些處理
//比如統(tǒng)一加token,對請求參數(shù)統(tǒng)一加密
request.interceptors.request.use(config => {
config.headers['Content-Type'] = 'application/json;charset=utf-8';
//let user = localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) :null
//config.headers['token'] = user ?.token ;//設(shè)置請求頭
return config
}, error => {
console.error('request error:'+ error) //for debug
return Promise.reject(error)
});
//response攔截器
//可以在接口響應(yīng)后統(tǒng)一處理結(jié)果
request.interceptors.response.use(
response => {
let res = response.data;
//兼容服務(wù)端返回的字符串?dāng)?shù)據(jù)
if (typeof res === 'string') {
res = res ? JSON.parse(res) : res
}
return res;
},
error => {
console.error('response error: ' + error) //for debug
return Promise.reject(error)
}
)
export default request
到了這里,關(guān)于“TypeError: utils request jS WEBPACK IMPORTED MODULE O .default is undefined‘報錯的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!