vue3 如何在 view 中使用子组件的变量?

制作了一个 demo https://sfc.vuejs.org/#eNpdUk...

App.vue

<script setup>
import { ref, computed } from 'vue'
import Hello from './Hello.vue';

const hello = ref(null)

const msg = ref('hello world')

function namef(){
  alert(hello.value.name)
}
  
</script>

<template>
  <Hello ref="hello"></Hello>
  <h1>{{ msg }}</h1>
  
  <h1>
    how to show hello.name here?
  </h1>
  
  <button @click="namef">
    namef
  </button>
</template>

Hello.vue

<template>
  component hello
</template>

<script setup>
  import { ref } from 'vue'
  const name = ref('the name')
  
  defineExpose({
    name
  })
</script>

如何才能在 App.vue 的 template 中显示 Hello.vue 的 name 值?

阅读 2.3k
2 个回答

要显示的地方直接 {{hello.name}}

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题