vue3父组件通过ref调用子组件的方法(组合模式,defineExpose)

 

Index.vue:

<script setup>
import { ref, onMounted } from 'vue'
import Child from './Child.vue'
import './index.css'

const child = ref(null)

onMounted(() => {
  child.value.handleGetValue()
})
</script>

<template>
  <div class="m-home-wrap">
    <Child ref="child"></Child>
    <div class="m-home-demo"></div>
  </div>
</template>

<style></style>

Child.vue:

<template>
  <div>child</div>
</template>

<script setup>
const handleGetValue = () => {
  console.log('子组件的方法')
}

defineExpose({
  handleGetValue
})
</script>

<style></style>

人工智能学习网站

https://chat.xutongbao.top

相关推荐

  1. vue组件直接调用组件方法通过ref

    2024-05-11 00:38:03       60 阅读
  2. Vue3+Ant Design 组件调用组件方法

    2024-05-11 00:38:03       32 阅读
  3. vue3组件调用组件方法

    2024-05-11 00:38:03       33 阅读
  4. vue组件调用组件方法

    2024-05-11 00:38:03       51 阅读
  5. Vue.js 中组件调用组件方法

    2024-05-11 00:38:03       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-11 00:38:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-11 00:38:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-11 00:38:03       82 阅读
  4. Python语言-面向对象

    2024-05-11 00:38:03       91 阅读

热门阅读

  1. QT day2

    QT day2

    2024-05-11 00:38:03      29 阅读
  2. 在Node.js中实现数据备份

    2024-05-11 00:38:03       30 阅读
  3. C++:左值(引用)&右值(引用)

    2024-05-11 00:38:03       34 阅读
  4. MySQL慢查询优化【二】

    2024-05-11 00:38:03       35 阅读
  5. Linux系统调用mmap

    2024-05-11 00:38:03       33 阅读
  6. 算法—四则运算

    2024-05-11 00:38:03       28 阅读
  7. c#读取bin文件

    2024-05-11 00:38:03       28 阅读