位置:首页 > Vue >

axios易用、简洁且高效的http库

字号+ 作者:micloud 来源:www.seoalphas.com 2020-12-20 08:15 浏览量:1804

微信截图_20201220083732.png

Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中。

1、支持node端和浏览器端--同样的API,node和浏览器全支持,平台切换无压力;

2、支持 Promise--使用Promise管理异步,告别传统callback方式

3、丰富的配置项--支持拦截器等高级配置

4、社区支持--axios相关的npm包数量一直在增长


安装:

npm install axios

vue cli3.x也可以使用下面的命令进行安装

vue add axios

具体的使用方法可以参考官方文档,这里只是记录下:

执行 GET 请求 :

axios.get('/user', {
    params: {
      ID: 12345
    }
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

执行 POST 请求 :

axios.post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

上面是基础的用法,当然相比ajax,也有更高级的用法。

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • vue点击事件@click.stop(阻止冒泡).native

    vue点击事件@click.stop(阻止冒泡).native

    浏览次数:8950

  • Vue+Element UI Radio默认选中问题 selected

    Vue+Element UI Radio默认选中问题 selected

    浏览次数:7926

  • vue设置网页页面title--router

    vue设置网页页面title--router

    浏览次数:2619

  • vue img标签:onerror="defaultImg"或@error=“defImg()” 使用

    vue img标签:onerror="defaultImg"或@error=“defImg()” 使用

    浏览次数:2512

网友点评
评论区域