【Vue】Vue 中如何使用 axios

1、命令行安装

npm install axios

2、在 main.js 中引入

import axios from 'axios'

3、在 main.js 中将 axios 改写为 vue 的原型属性 $http ,之后可在每个组件的 methods 中调用

Vue.prototype.$http=axios

4、在组件中使用

methods: {
      get(){
        this.$http({
          method:'get',
          url:'/url',
          data:{}
        }).then(function(res){
          console.log(res)
        }).catch(function(err){
          console.log(err)
        })
        
        this.$http.get('/url').then(function(res){
          console.log(res)
        }).catch(function(err){
          console.log(err)
        })
      }     
}          
发布了12 篇原创文章 · 获赞 14 · 访问量 6441

猜你喜欢

转载自blog.csdn.net/weixin_42678675/article/details/104608015
今日推荐