美文网首页前端Vue专辑Vue.jsvue
Vue3学习与实战 · 全局挂载使用Axios

Vue3学习与实战 · 全局挂载使用Axios

作者: 天問_专注于大前端技术 | 来源:发表于2021-11-24 22:00 被阅读0次

vue2中会习惯性的把axios挂载到全局,以方便在各个组件或页面中使用this.$http请求接口。但是在vue3中取消了Vue.prototype,在全局挂载方法和属性时,需要使用官方提供的globalPropertiesAPI。

Vue3 全局Axios

一、全局挂载

  • vue2项目中,入口文件main.js配置Vue.prototype挂载全局方法对象:
import Vue from 'vue'
import router from '@/router'
import store from '@vuex'
import Axios from 'axios'
import Utils from '@/tool/utils'
import App from './App.vue'

// ...

/* 挂载全局对象 start */
Vue.prototype.$http = Axios;
Vue.prototype.$utils = Utils;
/* 挂载全局对象 end */

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
  • vue3项目中,入口文件main.js配置globalProperties挂载全局方法对象:
import { createApp } from 'vue'
import router from './router'
import store from './store'
import Axios from 'axios'
import Utils from '@/tool/utils'
import App from './App.vue'

// ...

const app = createApp(App)

/* 挂载全局对象 start */
app.config.globalProperties.$http = Axios
app.config.globalProperties.$utils = Utils
/* 挂载全局对象 end */

app.use(router).use(store);
app.mount('#app')

二、全局使用

  • vue2中使用this.$http
<script>
  export default {
    data() {
      return {
        list: []
      }
    },
    mounted() {
      this.getList()
    },
    methods: {
      getList() {
        this.$http({
          url: '/api/v1/posts/list'
        }).then(res=>{
          let { data } = res.data
          this.list = data
        })
      },
    },
  }
</script>
  • vue3setup中使用getCurrentInstanceAPI获取全局对象:
<template>
  <div class="box"></div>
</template>
<script>
  import { ref, reactive, getCurrentInstance } from 'vue'
  export default {
    setup(props, cxt) {
      // 方法一 start
      const currentInstance = getCurrentInstance()
      const { $http, $message, $route } = currentInstance.appContext.config.globalProperties
      
      function getList() {
        $http({
          url: '/api/v1/posts/list'
        }).then(res=>{
          let { data } = res.data
          console.log(data)
        })
      }
      // 方法一 end

      // 方法二 start
      const { proxy } = getCurrentInstance()
      
      function getData() {
        proxy.$http({
          url: '/api/v1/posts/list'
        }).then(res=>{
          let { data } = res.data
          console.log(data)
        })
      }
      // 方法二 end

    }  
  }
</script>
  1. 方法一:通过getCurrentInstance方法获取当前实例,再根据当前实例找到全局实例对象appContext,进而拿到全局实例的config.globalProperties
  2. 方法二:通过getCurrentInstance方法获取上下文,这里的proxy就相当于this

提示: 可以通过打印getCurrentInstance()看到其中有很多全局对象,如:$route$router$store。如果全局使用了ElementUI后,还可以拿到$message$dialog等等。


《Vue3学习与实战》系列


欢迎访问:天问博客

相关文章

  • Vue3学习与实战 · 全局挂载使用Axios

    在vue2中会习惯性的把axios挂载到全局,以方便在各个组件或页面中使用this.$http请求接口。但是在vu...

  • axios 拦截器

    1、在 vue3 的项目中全局配置 axios 2、在 vue2 的项目中全局配置 axios 需要在 main....

  • vue2升级vue3: 全局变量挂载与类型声明

    全局变量挂载 vue2 Vue.prototype.$lm={} vue3 constapp=Vue.create...

  • axios的具体使用

    全局使用axios Axios的get post Axios全面配置的方式

  • Axios

    *axios官方文档 安装axios 入口文件 mian.js 中进行引入 / 挂载 具体使用 :

  • vue3全局挂载

    入口文件挂载 使用

  • axios全局使用

    vue中我们常用axios进行数据请求 axios模块在vue开发中如何使用引入,如何注册全局方法 一般情况下我们...

  • Vue爬坑之旅

    1.axios赋值的问题 描述:前后数据对接,使用nuxt整合的axios,使用vue中的钩子函数在页面组件挂载完...

  • axios 基本用法

    使用 npm: 或者 使用 bower: 或者直接使用 cdn: main.js设置如下 引入axios 挂载到v...

  • axios 配置

    axios 全局配置 3 种方法: 结合vue-axios使用 axios改写为Vue的原型属性 结合Vuex的a...

网友评论

    本文标题:Vue3学习与实战 · 全局挂载使用Axios

    本文链接:https://www.haomeiwen.com/subject/lxpotrtx.html