页面中经常使用 getCurrentInstance () 来获取 proxy ,不如封装一个方法搞定

创建 useCurrentInstance.ts 文件

import { ComponentInternalInstance, getCurrentInstance } from 'vue'
export default function useCurrentInstance() {
  const { appContext } = getCurrentInstance() as ComponentInternalInstance
  const proxy = appContext.config.globalProperties
  return {
    proxy
  }
}

组件内使用

<script lang="ts" setup>
import useCurrentInstance from "@/utils/useCurrentInstance";
const { proxy } = useCurrentInstance();
console.log(proxy);
</script>

Logo

前往低代码交流专区

更多推荐