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.2k
1 个回答
    onMounted(() => {
      const { proxy } = getCurrentInstance();

      // 使用全局方法
      proxy?.$getAuthorizationCode();
    });
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题