Vue中,axios是提供的一種數據請求的方式。
它一般通過三種方式來獲取:
1、npm的方式。
npm install --save axios
- 1
2、bower的方式
bower install --save axios
- 1
3、script引入的方式(cdn)
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
- 1
axios的api
- axios(config)
- axios(url [,config])
- axios.request(conig)
- axios.get(url [,config])
- axios.delete(url [,config])
- axios.head(url [,config])
- axios.post(url [,data , [,config]])
- axios.put(url [,data , [,config]])
- axios.patch(url [,data , [,config]])
axios的使用示例
demo1,axios.get()
axios.get('http://114.55.249.153:8028/goods/detail', {
params: {
id: 408
}
})
.then(function (response) {
console.log(response)
})
.catch(function (err) {
console.log(err)
})
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
demo2,axios.post()
axios.post('http://114.55.249.153:8028/goods/delete', {
id: 123333
})
.then(function (response) {
console.log(response)
})
.catch(function (error) {
console.log(error)
});
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
axios的併發請求API
- axios.all(sources);
- axios.spread(callback);
axios的併發請求示例
function get1(){
return axios.get('http://114.55.249.153:8028/goods/detail', {
params: {
id: 408
}
})
}
function get2(){
return axios.get('http://114.55.249.153:8028/goods/detail', {
params: {
id: 409
}
})
}
axios.all([get1(),get2()])
.then(axios.spread(function (acct, perms) {
// 兩個請求現在都執行完成
console.log('acct', acct)
console.log('perms', perms)
}))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21