vue3+ts 使用 getCurrentInstance

页面中经常使用 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>

猜你喜欢

转载自blog.csdn.net/m0_56274171/article/details/124302154