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

1.axios地址html

https://github.com/axios/axiosvue

2.安装ios

npm install axios

3.使用git

1.用在哪?github

main.js 全局

2.何时用?express

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

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

3.怎么用npm

数据从哪里来:axios

1.美团API接口 ,服务器

优势:贴近工做情形

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

2.本身搭建线上服务器数据异步

优势:贴近工做情形,本身造数据想怎玩怎么玩

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

3.搭建本地模拟数据

优势:贴近工做情形,本身造数据想怎玩怎么玩,访问速度快,稳定

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

结论:为了工做使用1,为了灵活性使用3,本身有现成的服务器或空间,从二、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组件添加数据交互功能,敬请关注。

相关文章
相关标签/搜索