axios学习笔记

Posted Archer-Fang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了axios学习笔记相关的知识,希望对你有一定的参考价值。

axios学习笔记
axios文档源地址:https://github.com/axios/axios
0.概念
axios 在NPM上的描述是:Promise based HTTP client for the browser and node.js。
axios是通过promise实现对ajax技术的一种封装,就像jQuery实现ajax封装一样。
简单来说: ajax技术实现了网页的局部数据刷新,axios实现了对ajax的封装。
1.Installing
$ npm install axios
2.Example
1.Performing a GET request
// Make a request for a user with a given ID
axios.get(‘/user?ID=12345‘)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
// Optionally the request above could also be done as
axios.get(‘/user‘, {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});

// 异步操作 Want to use async/await? Add the `async` keyword to your outer function/method.
async function getUser() {
try {
const response = await axios.get(‘/user?ID=12345‘);
console.log(response);
} catch (error) {
console.error(error);
}
}
NOTE: async/await is part of ECMAScript 2017 and is not supported in Internet Explorer and older browsers, so use with caution.
2.Performing a POST request
axios.post(‘/user‘, {
firstName: ‘Fred‘,
lastName: ‘Flintstone‘
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
3.多种并发请求
function getUserAccount() {
return axios.get(‘/user/12345‘);
}

function getUserPermissions() {
return axios.get(‘/user/12345/permissions‘);
}

axios.all([getUserAccount(), getUserPermissions()])
.then(axios.spread(function (acct, perms) {
// Both requests are now complete
}));
3.axios API(和ajax很像)
1.post
axios({
method: ‘post‘,
url: ‘/user/12345‘,
data: {
firstName: ‘Fred‘,
lastName: ‘Flintstone‘
}
});
2.get
axios({
method:‘get‘,
url:‘http://bit.ly/2mTM3nY‘,
responseType:‘stream‘
})
.then(function(response) {
response.data.pipe(fs.createWriteStream(‘ada_lovelace.jpg‘))
});
3.默认get方法
axios(‘/user/12345‘);
4.requireJs
4.1作用
防止JS阻塞浏览器渲染
4.2使用
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.16/require.min.js"></script>

 























































































以上是关于axios学习笔记的主要内容,如果未能解决你的问题,请参考以下文章

axios学习笔记

axios学习笔记

Axios学习笔记

vue3 学习笔记 —— axios 的使用有变化吗?

学习笔记Vue+Element UI+axios 实现简单的登录页面

VUE学习笔记:29.脚手架vue-cli之axios