美文网首页
13.vue2.0中的ajax

13.vue2.0中的ajax

作者: 白水螺丝 | 来源:发表于2017-06-20 13:31 被阅读286次

Vue 原本有一个官方推荐的 ajax 插件 vue-resource,但是自从 Vue 更新到 2.0 之后,官方就不再更新 vue-resource,但是也可以正常的使用它。

目前主流的 Vue 项目,都选择 axios 来完成 ajax 请求。

使用 cnpm 安装 axios

npm install axios -save

安装其他插件的时候,可以直接在 main.js 中引入并 Vue.use(),但是 axios 并不能 use,只能每个需要发送请求的组件中即时引入

具体的demo如下


// 发送一个get请求
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);
  });


//发送一个post请求

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

//发送多个请求
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
  }));

相关文章

  • 13.vue2.0中的ajax

    Vue 原本有一个官方推荐的 ajax 插件 vue-resource,但是自从 Vue 更新到 2.0 之后,官...

  • JS中的Ajax

    JS中的Ajax Ajax简介 Ajax为 Asynchronous Javascript And XML 的缩写...

  • HTML5学习小记二ajax,window.orientatio

    1.ajax的学习 HTML5中的ajax相当于iOS中的afnetworking;详见jQuery ajax -...

  • 原生ajax和jquery中的ajax

    原生的ajax请求方法: jquery中的ajax:

  • face14ajax基础内容

    ajax基础内容 ajax技术利用了 什么协议?简述ajax的工作机制 写出jquery中,可以处理ajax的几种...

  • AJAX请求

    原生JavaScript中的AJAX请求 AJAX = Asynchronous JavaScript and X...

  • Ajax-03

    jQuery 中的 Ajax $.ajax()方法概述 作用:发送Ajax请求。 可替换属性 作用:发送jsonp...

  • Django中的Ajax

    一、原生Ajax 二、Jquery中的Ajax 1、$.ajax():type为POST时,表示发送数据,type...

  • ajax小结

    在开发过程中ajax是必须的,对近期在ajax的使用上的心得进行总结。 一、关于ajax AJAX = Async...

  • AJAX入门

    AJAX(Async Javascript and Xml):在AJAX中的异步不是异步编程中的异步,而是泛指“局...

网友评论

      本文标题:13.vue2.0中的ajax

      本文链接:https://www.haomeiwen.com/subject/osboqxtx.html