ythy / blog

Give everything a shot
6 stars 0 forks source link

VUE `h()`方法创建控件无法获取上下文 #514

Open ythy opened 1 year ago

ythy commented 1 year ago

应当手动传入 appContext

const vNode = h(XXX)
vNode.appContext = context

如何在setup控件中获取appContext

  1. import { getCurrentInstance } from 'vue'
  2. onMount() 中获取实例保存在ref中使用, 在onMount()外取不到
    import { ref, onMounted, getCurrentInstance } from 'vue'
    export default function useContext() {
    const context = ref()
    onMounted(() => (context.value = getCurrentInstance()?.appContext))
    }