当前位置: 首页 > news >正文

网站开发 项目的人员分配百度搜索链接入口

网站开发 项目的人员分配,百度搜索链接入口,找建设网站公司吗,人才网站建设的目标在 Vue 3 中&#xff0c;父组件点击按钮触发子组件事件有以下三种常用方式&#xff1a; 方法 1&#xff1a;使用 ref 直接调用子组件方法&#xff08;推荐&#xff09; vue 复制 下载 <!-- 父组件 --> <template><button click"callChildMethod"…

在 Vue 3 中,父组件点击按钮触发子组件事件有以下三种常用方式:

方法 1:使用 ref 直接调用子组件方法(推荐)

vue

复制

下载

<!-- 父组件 -->
<template><button @click="callChildMethod">父组件按钮</button><ChildComponent ref="childRef" />
</template><script setup>
import { ref } from 'vue';
import ChildComponent from './ChildComponent.vue';const childRef = ref(null);function callChildMethod() {if (childRef.value) {childRef.value.childMethod(); // 直接调用子组件方法}
}
</script>

vue

复制

下载

<!-- 子组件 ChildComponent.vue -->
<script setup>
// 暴露给父组件的方法
const childMethod = () => {console.log('子组件方法被调用');// 执行子组件逻辑
};// 暴露方法给父组件
defineExpose({childMethod
});
</script>

方法 2:通过 Props 传递回调函数

vue

复制

下载

<!-- 父组件 -->
<template><button @click="triggerChild">父组件按钮</button><ChildComponent :parentCallback="callback" />
</template><script setup>
import ChildComponent from './ChildComponent.vue';const callback = () => {console.log('父组件的回调函数被执行');
};function triggerChild() {// 通过触发子组件事件间接执行// 实际执行逻辑在子组件内
}
</script>

vue

复制

下载

<!-- 子组件 -->
<template><!-- 接收父组件传递的回调 -->
</template><script setup>
const props = defineProps(['parentCallback']);// 子组件内执行回调
function executeParentCallback() {if (props.parentCallback) {props.parentCallback();}
}// 暴露方法供父组件调用
defineExpose({ executeParentCallback });
</script>

方法 3:使用自定义事件(子组件触发)

vue

复制

下载

<!-- 父组件 -->
<template><button @click="emitEvent">父组件按钮</button><ChildComponent @child-event="handleEvent" />
</template><script setup>
import ChildComponent from './ChildComponent.vue';function emitEvent() {// 触发自定义事件(实际由子组件监听)
}function handleEvent(data) {console.log('收到子组件事件:', data);
}
</script>

vue

复制

下载

<!-- 子组件 -->
<script setup>
const emit = defineEmits(['child-event']);// 当需要执行时触发事件
function triggerEvent() {emit('child-event', { data: '子组件数据' });
}defineExpose({ triggerEvent });
</script>

推荐方案对比

方法优点适用场景
ref 直接调用直接高效,逻辑清晰父组件直接控制子组件特定操作
Props 回调函数符合单向数据流需要传递数据到父组件的情况
自定义事件符合组件解耦原则子组件主动通知父组件的场景

最佳实践建议

  1. 需要直接控制子组件行为时 → 使用 ref 方法

  2. 需要子组件返回数据时 → 使用 Props 回调

  3. 实现组件解耦时 → 使用自定义事件

根据你的具体场景选择最合适的方式,通常 ref 直调是最直接高效的解决方案。

http://www.hkea.cn/news/730633/

相关文章:

  • 图片设计在线网站推广优化外包便宜
  • 武汉平价做网站网络软文推广案例
  • 新产品线上推广方案鞍山seo外包
  • 网站建网站建设和优佛山网络推广培训
  • 毕业设计做网站怎么样微信crm管理系统
  • 个人网站开发多少钱电脑培训班零基础
  • 互联网有哪些岗位宁波免费seo在线优化
  • 惠州做棋牌网站建设哪家技术好哪里的网络推广培训好
  • 如何做线上赌博的网站推广策略有哪些方法
  • 男的女的做那个视频网站百度收录需要多久
  • 大通县wap网站建设公司网站免费制作
  • 哪个网站教做公众号甘肃百度推广电话
  • 网站怎么让百度收录广告网络推广
  • 小型网站设计及建设论文定制网站制作公司
  • 视频网站建设费用排名优化网站seo排名
  • 怎么自己做网站服务器linux百度账号查询
  • 梧州网站推广方案百度热搜 百度指数
  • 网站不兼容ie6自助建站模板
  • 甘肃网站建设公司百中搜优化软件
  • 国内外贸网站建设公司seo教程 百度网盘
  • 一物一码二维码生成系统最好用的系统优化软件
  • 如何在大网站做外链镇江网站建站
  • 杭州网站建设公司导航短视频营销案例
  • 昆明做网站建设有哪些长尾关键词排名工具
  • 一女被多男做的视频网站网站seo系统
  • 网站建设 青海网站建设找哪家好
  • win7 网站配置优化方案官网电子版
  • 广州seo优化公司排名浙江seo博客
  • 全网推广的方式有哪些抖音seo推荐算法
  • 网站开发开源架构抖音营销软件