当前位置:首页 > 操作系统 > Ios

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);
  }
});
    

原文:https://www.cnblogs.com/wulzt/p/9531836.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!