码迷,mamicode.com
首页 > 移动开发 > 详细

vue.js中使用Axios

时间:2017-07-31 00:55:38      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:代码   comment   call   赋值   axios   cat   console   ios   nbsp   

Axios为vue2.0官方推荐HTTP请求工具,之前的是vue-resource

在使用的过程中总结了两种使用方式:

  1.和vue-resource使用类似

    引入:import axios from ‘axios‘;

       Vue.prototype.$http = axios;

    使用:this.$http.get(URL).then(response => { // success callback }, response => { // error callback });

  2.在你需要的组件中使用

    引入:import axios from ‘axios‘;

    使用:axios.get(URL).then(response => { // success callback }, response => { // error callback });

注:在将response返回值赋值给data是一直不成功,之前的代码:

    axios.get()
    .then(function (response) {
      if (response.data.errno === ERR_OK) {
        this.seller = response.data.data;
      }
      console.log(response.data.data);
    })
    .catch(function (error) {
      console.log(error);
    });

  现在改为es6语法规范就好了,现在的代码:

    axios.get(‘/api/seller‘).then(response => {
      if (response.data.errno === ERR_OK) {
        this.seller = response.data.data;
      }
    }, response => {
      console.log(response);
    });

 

vue.js中使用Axios

标签:代码   comment   call   赋值   axios   cat   console   ios   nbsp   

原文地址:http://www.cnblogs.com/magiczjk/p/7260885.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!