标签:触发 const faEvent 步骤 Vue3 组件 childEvent emit
父组件
<template> <el-button type="primary" @click="faEvent">父组件</el-button> <hr /> <!-- 父2子 步骤 1. 定义ref --> <!-- 子2父 步骤 3. 父组件创建自定义事件 --> <SonCpt ref="sonRef" @childEvent="childEvent" /> </template> <script> import { ref } from "vue"; import SonCpt from "./son.vue"; export default { components: { SonCpt }, setup() { // 父2子 步骤 2. 获取引用 const sonRef = ref(); const faEvent = () => { // 父2子 步骤 4. 在某个时机触发子组件方法 sonRef.value.faEvent("我是写在index组件的方法"); }; // 子2父 步骤 4. 父组件创建对应的方法 const childEvent = (v) => { console.log("index里面的函数", v); }; // 父2子 步骤 3. 导出引用, 不导出获取的引用是undefined // 子2父 步骤 7. 父组件导出方法 return { sonRef, faEvent, childEvent }; }, }; </script> <style> </style>
子组件
<template> <el-button type="danger" @click="childEvent">子组建</el-button> </template> <script> export default { props: ["msg"], // 子2父 步骤 1. 结构emit setup(props, { emit }) { // 父2子 步骤 5. 自组建定义方法 const faEvent = (v) => { console.log("子组建的方法, 接收到的内容为:" + v); }; const childEvent = () => { // 子2父 步骤 2.使用emit触发父组件方法 emit("childEvent", "我是写在son里面的msg"); }; // 父2子 步骤 6. 自组建需要导出该方法 return { faEvent, childEvent }; }, }; </script> <style> </style>
标签:触发,const,faEvent,步骤,Vue3,组件,childEvent,emit 来源: https://www.cnblogs.com/fmg0224/p/16439720.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。