Vue实战—项目数据交互-axios(4)

栏目: jQuery · 发布时间: 4年前

内容简介:1.axios地址2.安装

1.axios地址

https://github.com/axios/axios

2.安装

npm install axios

3.使用

1.用在哪?

main.js 全局

2.什么时候用?

生命周期:https://cn.vuejs.org/v2/guide/instance.html

    created 的时候 发起异步请求获取数据

3.怎么用

数据从哪里来:

1.美团API接口 ,

优点:贴近工作情形

缺点:官方接口申请有门槛,手续繁琐,限制多,速度慢,非官方不稳定,容易失效

2.自己搭建线上服务器数据

优点:贴近工作情形,自己造数据想怎玩怎么玩

缺点:写JSON麻烦,不稳定,速度慢,域名空间收费,繁琐

3.搭建本地模拟数据

优点:贴近工作情形,自己造数据想怎玩怎么玩,访问速度快,稳定

缺点:写JSON、配置服务器麻烦(如使用express或者mockjs)

结论:为了工作使用1,为了灵活性使用3,自己有现成的服务器或空间,从2、3中选的话,选择2

我们使用1,按照工作标准来:

美团外卖官方接口:

http://developer.waimai.meitu...

GET例子:

axios.get('/user?ID=12345')
  .then(function (response) {
    // handle success
    console.log(response);
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  })
  .then(function () {
    // always executed
  });

POST例子:

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

总 结

本文我们从安装,到axios,让我们对vue的数据相关的技术点有了一定了解,我们将一步步完善项目,丰富我们的技术经验。

下篇:我们通过为项目header组件添加数据交互功能,敬请关注。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

C++ Concurrency in Action

C++ Concurrency in Action

Anthony Williams / Manning Publications / 2012-2-28 / USD 69.99

HIGHLIGHT C++ Concurrency in Action is the first book to market to show how to take advantage of the new C++ Standard and how to write robust multi-threaded applications in C++. DESCRIPTION With ......一起来看看 《C++ Concurrency in Action》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具