-
axios并行請(qǐng)求,然后統(tǒng)一處理。
axios.spread() 的參數(shù)個(gè)數(shù)等于axios的請(qǐng)求數(shù)。
axios.all([ ????axios.get('/data.json'), ????axiso.get('/city.json') ]).then( ????axios.spread((dataRes,?cityRes)?=>?{ ????????console.table(dataRes,?cityRes) ????} )
查看全部 -
get請(qǐng)求? Query String?
post請(qǐng)求
查看全部 -
let formData = new FormData();
for(let key in data){
formData.append(key,value);
}
//表單提交
axios.post("/post",formData).then(res=>{
console.log(res);
})
查看全部 -
axios.post() 三個(gè)參數(shù),分別是,url路徑,請(qǐng)求的數(shù)據(jù)data,config
data有兩種:
form-data 表單提交(圖片、文件上傳)
application/json? ?
查看全部 -
注意真實(shí)項(xiàng)目中router.js里面路由的寫法:
{
??????path:?'/',
??????name:?'HelloWorld',
??????component:?HelloWorld//第一種方式
????},
????{
??????path:?'/axios',
??????name:?'axios',
??????//?component:?Axios
??????component:()=>import('../components/Axios')//第二種方式,按需引入真實(shí)項(xiàng)目使用
????}
查看全部 -
axios.all([]).then(axios.spread(datdres,citydres)=>{})查看全部
-
axios配置參數(shù)
baseURL:'http://localhost:8080',//請(qǐng)求的域名,基本地址 timeout:1000,//請(qǐng)求的超時(shí)時(shí)長,單位毫秒1000=1s url:'json/shouji.json'//請(qǐng)求的路徑 method:'get,put',//get,put,post,patch,delete請(qǐng)求方法 headers:{ ????token:'' },//設(shè)置請(qǐng)求頭 params:{},//請(qǐng)求參數(shù)拼接在URL上 data:{},//請(qǐng)求參數(shù)放在請(qǐng)求體
可以設(shè)置參數(shù)的地方:
1.axios全局配置
axios.defaults.timeout=1000
2.axios實(shí)例配置
let instance=axios.create()//不設(shè)置.默認(rèn)使用全局配置.
instance.defaults.timeout=3000//可以在修改
3.axios請(qǐng)求配置
instance.get('/data.json',{timeout:5000})
查看全部 -
axios實(shí)例
類似數(shù)組創(chuàng)建方式兩種:
?let instance = this.$http.create({
? ? ? ? ? ? ? ? baseURL:'http://localhost:8080',
? ? ? ? ? ? ? ? timeout:1000
? ? ? ? ? ? })
為什么要?jiǎng)?chuàng)建axios實(shí)例:后端接口地址有多個(gè),并且超時(shí)時(shí)長不一樣
查看全部 -
并發(fā)請(qǐng)求:同時(shí)進(jìn)行多個(gè)請(qǐng)求,并統(tǒng)一處理返回值
axios.all()
axios.spread()
查看全部 -
axios請(qǐng)求方法:get,post,put,patch,delete
get:獲取數(shù)據(jù)
post:提交數(shù)據(jù) (表單提交+文件上傳)
put:更新數(shù)據(jù)(所有數(shù)據(jù)推送到服務(wù)端(后端))
patch:更新數(shù)據(jù)(只將修改的數(shù)據(jù)推送到后端)
delete:刪除數(shù)據(jù)
查看全部 -
axios的特性:
1.支持Promise API
2.攔截請(qǐng)求和響應(yīng)
3.轉(zhuǎn)換請(qǐng)求數(shù)據(jù)和響應(yīng)數(shù)據(jù)
4.取消請(qǐng)求 5.自動(dòng)轉(zhuǎn)換json數(shù)據(jù) 6.客戶端支持防御XSRF攻擊
查看全部 -
取消正在進(jìn)行的http請(qǐng)求
查看全部 -
實(shí)際開發(fā)中一般添加同意的錯(cuò)誤處理
查看全部 -
let instance = axios.create({
baseURL:"http:localhost:8080",
tieout:3000
});
instance.get('/data.json')
超時(shí)報(bào)錯(cuò)401
查看全部 -
后端接口地址有多個(gè),并且超時(shí)時(shí)間不一樣
查看全部
舉報(bào)