iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >Vue3全局挂载使用Axios学习实战
  • 874
分享到

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

2024-04-02 19:04:59 874人浏览 八月长安
摘要

目录引言一、全局挂载二、全局使用引言 在Vue2中会习惯性的把axiOS挂载到全局,以方便在各个组件或页面中使用this.$Http请求接口。但是在vue3中取消了Vue.proto

引言

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

一、全局挂载

  • 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'
// ...

Vue.prototype.$http = Axios;
Vue.prototype.$utils = Utils;

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)

app.config.globalProperties.$http = Axios
app.config.globalProperties.$utils = Utils

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>
  • 方法一:通过getCurrentInstance方法获取当前实例,再根据当前实例找到全局实例对象appContext,进而拿到全局实例的config.globalProperties
  • 方法二:通过getCurrentInstance方法获取上下文,这里的proxy就相当于this

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

以上就是Vue3全局挂载使用Axios学习实战的详细内容,更多关于Vue3全局挂载使用Axios的资料请关注编程网其它相关文章!

--结束END--

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

本文链接: https://www.lsjlt.com/news/153054.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作