vue中axios的引入和使用

axios的官网地址:http://www.axios-js.com/docs/vue-axios.html

npm方式安装axios : npm i axios vue-axios

axios的引入:

utils/axios-----文件目录
// utils: 小工具

// 引入相关的模块
import Vue from 'vue' 
import axios from 'axios'
import VueAxios from 'vue-axios'

Vue.use(VueAxios, axios) //参数顺序固定, 不可颠倒
// 当前文件必须在 main.js 中引入, 才能触发效果

main.js的引入

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
// 引用axios,触发其中的代码, .js后缀名可以省略
import './utils/axios

Vue.config.productionTip = false
new Vue({
  router,
  store,
  render: h => h(App),
}).$mount('#app')

页面中使用:

  methods: {
    getData() {
      let url = 'http://XXXXXXXXXXX'
      // 成功触发then, 失败触发catch
      this.axios
       .get(url)
       .then(res => {
          console.log(res)
          // 远程存本地
          this.data = res.data
       })
       .catch(err => console.log(err))
   },
 },

页面中使用axios也可以用如下请求方式:

 getDate() {

      let that = this;

      this.axios({

        method: "get",

        url: "XXXXXX",

        params: {},

      }).then(

        (res) => { },

        (err) => { }

      );

    },

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值