使用Vue3全局变量在组件中无法获取变量

在Vue3官方文档中说 使用 globalPropertiesgetCurrentInstance 在全局中注册和使用变量,其语法等同 Vue2 中的 Vue.prototype.xxx

在 main.js 中注册全局变量

import { createApp } from 'vue'
import axios from "axios"
const app = createApp(App)
app.config.globalProperties.$axios = axios

在其他组件使用 axios, 注意有些文档中会将 proxy 写成 ctx ,但是亲测 ctx 中无法取得全局变量* ,此时的 proxy 相当于 this.$axios

import { getCurrentInstance } from "vue"
export default {
   name:"component",
   setup(){
       const {proxy} = getCurrentInstance()
       const gitData = async ()=>{
           let response = await proxy.$axios({
               method:"get",
               url:""
          })
           console.log(proxy)
      }
  }
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇