vue-cli中使用axios

安装

npm install axios --save-dev

使用方式

  • 引入包
import axios from 'axios'
  • axios不是vue的插件,不能使用Vue.use(). 要通过控制原型链的方式来引入。
Vue.prototype.$http = axios
  • 在项目中使用
          this.$http.get('https://erienniu.xyz/api/sidebar')
            .then(function(response) {
              console.log(response.data)
            })
            .catch(function(error) {
              console.log(error)
            })

跨域问题需要用jsonp时

https://github.com/axios/axios/blob/master/COOKBOOK.md#jsonp

$ npm install jsonp --save
const jsonp = require('jsonp');

jsonp('http://www.example.com/foo', null, (err, data) => {
  if (err) {
    console.error(err.message);
  } else {
    console.log(data);
  }
});
posted @ 2018-08-24 20:11  五个唔  阅读(160)  评论(0编辑  收藏  举报