vue3之axios配置(阿飞)

1.使用npm i axios --save

2.全局使用 main.js中

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import { showMixin } from './mixins/index'
// import axios from 'vue-axios'
import axios from 'axios'
// import VueCompositionApi from '@vue/composition-api'
// createApp(App).config.productionTip = false
// createApp(App).use(store).use(router).use(VueCompositionApi).mount('#app')
// =========配置minxin
// createApp(App).mixin(showMixin).use(store).use(router).mount('#app')
// ===========配置axios
const app = createApp(App)
axios.defaults.baseURL = 'http://www.kymid.com/testphp/public/index.php/api/'
app.config.globalProperties.$http = axios
app.mixin(showMixin).use(store).use(router).mount('#app')

3.在组件中使用

<template>
  <div class="about">
    <h1>This is an about page</h1>
    <button @click="mymixin">测试mixin</button>
    <button @click="fn">测试mixin</button>
  </div>
</template>
<script>
// import { showMixin } from '../mixins/index'
// export default {
//   mixins: [showMixin]
// }
  export default {
    created () {
      // this.$http.get('http://www.kymid.com/testphp/public/index.php/api/apis/about_get?lang=en').then(val => {
      this.$http.get('apis/about_get?lang=en').then(val => {
        console.log(val)
      })
    }
  }
</script>

方法二:在vue3中,新的组合式[API]没有this,那我们如果需要用到this怎么办?

getCurrentInstance 方法获取当前组件的实例,然后通过 ctxproxy 属性获得当前上下文,这样我们就能在setup中使用router和vuex了

import { getCurrentInstance, onMounted } from 'vue'
 setup (props, context) {
      // 在
      const { proxy } = getCurrentInstance()
      onMounted(() => {
        getData()
      })
      function getData () {
        proxy.$http.get('apis/about_get?lang=en').then(res => {
          console.log(res)
        })
      }
    }

方法三:使用provide 和 inject 方法共享

main.js中:

const app = createApp(App)
axios.defaults.baseURL = 'http://www.kymid.com/testphp/public/index.php/api/'
app.config.globalProperties.$http = axios
// 方法三:
app.provide('$axios', axios)

组件中:

import { getCurrentInstance, onMounted, inject } from 'vue'
// import { inject } from 'vue'
  export default {
    created () {
      // 方式一: 在created中 直接使用this调用
      // this.$http.get('http://www.kymid.com/testphp/public/index.php/api/apis/about_get?lang=en').then(val => {
      this.$http.get('apis/about_get?lang=en').then(val => {
        console.log(val)
      })
    },
    setup (props, context) {
      // 在
      const { proxy } = getCurrentInstance()
      onMounted(() => {
        getData()
      })
      function getData () {
        proxy.$http.get('apis/about_get?lang=en').then(res => {
          console.log(res)
        })
      }
      // 方法三: 使用共享
      const $axios = inject('$axios')
      $axios.get('apis/about_get?lang=en').then((resp) => {
        console.log(resp)
      })
    }
  }

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值