Skip to content

[Vue 3.4] App 中的 _instance 问题 #10824

Discussion options

You must be logged in to vote

原因

没有在子组件中defineExpose instance属性,所以你在父组件通过ref访问不到。

推荐用法

在线例子

父组件:

<script setup>
import {
  h,
  ref,
  createApp,
  onMounted,
  onBeforeUnmount
} from 'vue'
import Com from './Comp.vue'

const appRef = ref()

const handle = () => {
  // instance是我在子组件Comp.vue中expose出去的
  console.warn(appRef.value.instance)
}

onMounted(() => {
  const demo = document.querySelector('#demo')
  demo.addEventListener('click', handle)

  const app = createApp({
    render() {
      return h(Com, { ref: appRef })
    }
  })
  app.mount(demo)
})

onBeforeUnmount(() => {
  const demo = document.querySelector('#demo')
  demo.removeEventListener('click', handle)
})

</script>

<template>
  <button @click="appRef.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by SnobbyVirus1973
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants