vue3 全局方法报错?: Cannot destructure property 'proxy' of 'getCurrentInstance(...)' as it is null?

main.js

const app = createApp(App)
app.config.globalProperties.$getAuthorizationCode = getAuthorizationCode;

组件中

import {
  getCurrentInstance,
} from "vue";  
const { proxy } = getCurrentInstance();
//使用
   proxy?.$getAuthorizationCode();

提示错误 当前项目环境是js,应该怎么调整呢?

Cannot destructure property 'proxy' of 'getCurrentInstance(...)' as it is null.
阅读 14.3k
1 个回答
    onMounted(() => {
      const { proxy } = getCurrentInstance();

      // 使用全局方法
      proxy?.$getAuthorizationCode();
    });
推荐问题