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

Index.vue:

javascript 复制代码
<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:

javascript 复制代码
<template>
  <div>child</div>
</template>

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

defineExpose({
  handleGetValue
})
</script>

<style></style>

人工智能学习网站

https://chat.xutongbao.top

相关推荐
whyfail9 小时前
Vue原理(暴力版)
前端·vue.js
踢球的打工仔9 小时前
jquery的基本使用(3)
前端·javascript·jquery
徐同保9 小时前
js 点击按钮 把文本转成文件并下载下来
开发语言·javascript·ecmascript
VX:Fegn089510 小时前
计算机毕业设计|基于springboot + vue敬老院管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
bug总结10 小时前
前端开发中为什么要使用 URL().origin 提取接口根地址
开发语言·前端·javascript·vue.js·html
程序员爱钓鱼11 小时前
Node.js 编程实战:数据库连接池与性能优化
javascript·后端·node.js
老华带你飞11 小时前
建筑材料管理|基于springboot 建筑材料管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习·spring
Gomiko11 小时前
JavaScript DOM 原生部分(二):元素内容修改
开发语言·javascript·ecmascript
一招定胜负11 小时前
网络爬虫(第三部)
前端·javascript·爬虫
Data_agent11 小时前
实战:用Splash搞定JavaScript密集型网页渲染
开发语言·javascript·ecmascript