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

关于网站建设与维护的参考文献cps推广平台

关于网站建设与维护的参考文献,cps推广平台,苏州建网站哪家,百度网站开发业务在 Vue 3 中使用 emit&#xff0c;子组件可以将事件通知父组件&#xff0c;父组件可以在响应这些事件时执行特定的逻辑。 emit 是一种非常灵活的通信方式&#xff0c;允许组件之间以解耦的方式进行交互。 1. 基本用法 1、使用 defineEmits 子组件 <template><div…

在 Vue 3 中使用 emit,子组件可以将事件通知父组件,父组件可以在响应这些事件时执行特定的逻辑。

emit 是一种非常灵活的通信方式,允许组件之间以解耦的方式进行交互。

1. 基本用法

1、使用 defineEmits

子组件

<template><div style="border: 1px solid orange"><p>Child</p><button @click="handleClick" style="border: none;">Click Me</button></div>
</template><script setup>
import { defineEmits } from 'vue'
const emit = defineEmits(['update']) // 定义事件
const handleClick = () => {emit('update', 'newValue') // 触发 'update' 事件,并传递参数
}
</script>

父组件

<template><div style="border: 1px solid red"><p>Father</p><Child @update="handleUpdate" /></div>
</template><script setup>
import Child from './Child.vue'const handleUpdate = (value) => {console.log('Received from child:', value)
}
</script>

2、在模板中直接使用 emit 

子组件

<template><div style="border: 1px solid orange"><p>Child</p><button @click="$emit('update', 'newValue')" style="border: none;">Click Me</button></div>
</template>

父组件不变 

3、在 JSX/TSX 中使用 emit 

子组件

import { defineComponent } from 'vue';export default defineComponent({setup(_, { emit }) {const handleClick = () => {emit('update', 'newValue');};return () => (<div style="border: 1px solid orange"><p>Child</p><button style="border: none;" onClick={handleClick}>Click Me</button></div>);}
});

父组件

import { defineComponent } from 'vue';
import Child from './Child';const Father = defineComponent({setup() {const handleUpdate = (value) => {console.log('Received from child:', value)}return () => (<div style="border: 1px solid red"><p>Father</p><Child onUpdate={handleUpdate} /></div>);},
});export default Father;

2. 举例 🌰

子组件

import { defineComponent } from 'vue';const Child = defineComponent({props: ['label', 'message'],emits: {'update': (value: string) => typeof value === 'string','click': null,},setup(props, { emit }) {const emitEvent = () => {emit('update', 'Hello from Child!');emit('click');};return () =><div style={{ border: '1px solid pink' }}><h3>Child Component Content</h3><button onClick={emitEvent} style={{ border: 'none' }}>{props.label}</button><div>props.message:{props.message}</div></div>},
});export default Child;

父组件

import { defineComponent, ref } from 'vue';
import Child from './Child';const Father = defineComponent({setup() {const date = ref('2024-08-21');const handleUpdate = (target: string) => {console.log('Update event received:', target);};const handleClick = () => {console.log('Click event received');};return () => (<div><Child label="Click Me" message={date.value} onUpdate={handleUpdate} onClick={handleClick} /></div>);},
});export default Father;

父组件向子组件传递数据,子组件使用 prop 接收,进而展示到页面。

子组件向父组件抛出事件 emit,并且可以传递参数,父组件使用 onXXX 来监听子组件触发的事件。比如 onUpdate 和 onClick 事件。

3. 注意事项

1、事件名称区分大小写

Vue 3 中的事件名称是区分大小写的。这意味着 @update 和 @Update 是两个不同的事件名称。在子组件和父组件中,确保事件名称的一致性非常重要。

2、事件参数传递

使用 emit 可以传递多个参数,这些参数将在父组件中对应的事件处理函数中接收到,并且需要按照顺序正确接收它们。

// 子组件
emit('update', 'value1', 'value2');// 父组件
<Child @update="handleUpdate" /><script setup>
const handleUpdate = (param1, param2) => {console.log(param1, param2); // param1 = 'value1', param2 = 'value2'
};
</script>

3、事件参数类型

在使用 TypeScript 时,defineEmits 可以定义事件和参数的类型。不仅可以提高代码的安全性,还能在开发过程中获得更好的类型提示。

const emit = defineEmits<{ (event: 'update', value: string): void }>();emit('update', 'newValue'); // 正确
emit('update', 123); // 错误,类型不匹配

4、确保事件已声明

通过 defineEmits 定义子组件中的事件时,要确保所有可能触发的事件都已声明。未声明的事件将无法通过 emit 触发。

const emit = defineEmits(['update', 'delete']);emit('update', 'newValue'); // 正确
emit('delete'); // 正确
emit('add'); // 错误,未声明 'add' 事件

5、避免滥用事件

在设计组件时,尽量减少事件的种类和数量,尤其是在组件树较为复杂时。过多的事件可能导致代码难以维护和调试。


文章转载自:
http://dinncohorsemanship.tqpr.cn
http://dinncosquish.tqpr.cn
http://dinncoquattrocento.tqpr.cn
http://dinncoinsincerity.tqpr.cn
http://dinncolugansk.tqpr.cn
http://dinncoxerosis.tqpr.cn
http://dinncoholophone.tqpr.cn
http://dinncomagnitogorsk.tqpr.cn
http://dinncosalesite.tqpr.cn
http://dinncoconic.tqpr.cn
http://dinncoscrambler.tqpr.cn
http://dinncohydronitrogen.tqpr.cn
http://dinncoaegean.tqpr.cn
http://dinncounfinishable.tqpr.cn
http://dinncogoest.tqpr.cn
http://dinncovologda.tqpr.cn
http://dinncoparty.tqpr.cn
http://dinncoks.tqpr.cn
http://dinncosplashdown.tqpr.cn
http://dinncoperiods.tqpr.cn
http://dinncofrequency.tqpr.cn
http://dinncoprolapse.tqpr.cn
http://dinncobosket.tqpr.cn
http://dinncomidlife.tqpr.cn
http://dinncogeosyncline.tqpr.cn
http://dinncocacography.tqpr.cn
http://dinncoostler.tqpr.cn
http://dinncoundrew.tqpr.cn
http://dinnconucleant.tqpr.cn
http://dinncocrownet.tqpr.cn
http://dinncorubato.tqpr.cn
http://dinncosoniferous.tqpr.cn
http://dinncobucket.tqpr.cn
http://dinncowhir.tqpr.cn
http://dinncosuperscript.tqpr.cn
http://dinncounstring.tqpr.cn
http://dinncosnobling.tqpr.cn
http://dinncoretribalize.tqpr.cn
http://dinncodreibund.tqpr.cn
http://dinncooverweigh.tqpr.cn
http://dinncomastoidectomy.tqpr.cn
http://dinncoameloblast.tqpr.cn
http://dinncohepatin.tqpr.cn
http://dinncoscabland.tqpr.cn
http://dinncothickleaf.tqpr.cn
http://dinncooverstory.tqpr.cn
http://dinncoborough.tqpr.cn
http://dinncomatronhood.tqpr.cn
http://dinncopromises.tqpr.cn
http://dinncoabsurdly.tqpr.cn
http://dinncobackstabber.tqpr.cn
http://dinncobelee.tqpr.cn
http://dinncoblastopore.tqpr.cn
http://dinncogirdlecake.tqpr.cn
http://dinncoundulance.tqpr.cn
http://dinncoconsumerism.tqpr.cn
http://dinncoisopropanol.tqpr.cn
http://dinncobarbuda.tqpr.cn
http://dinncoalveoli.tqpr.cn
http://dinnconitroso.tqpr.cn
http://dinncosilphid.tqpr.cn
http://dinncoprimula.tqpr.cn
http://dinncophilobiblic.tqpr.cn
http://dinncofumigate.tqpr.cn
http://dinncofluorimetric.tqpr.cn
http://dinncomicroreproduction.tqpr.cn
http://dinncoessie.tqpr.cn
http://dinncotheological.tqpr.cn
http://dinncotophet.tqpr.cn
http://dinncodeintegro.tqpr.cn
http://dinncohomogenize.tqpr.cn
http://dinncoalmsman.tqpr.cn
http://dinncosubsequence.tqpr.cn
http://dinncodulcitone.tqpr.cn
http://dinncovenally.tqpr.cn
http://dinncodogrobber.tqpr.cn
http://dinncoboarder.tqpr.cn
http://dinncopurport.tqpr.cn
http://dinncoechinococcus.tqpr.cn
http://dinncohoe.tqpr.cn
http://dinncofadayeen.tqpr.cn
http://dinncohobodom.tqpr.cn
http://dinncobusinessman.tqpr.cn
http://dinncoaccommodable.tqpr.cn
http://dinncogangsa.tqpr.cn
http://dinncotassy.tqpr.cn
http://dinncoearthshine.tqpr.cn
http://dinncou.tqpr.cn
http://dinncoxms.tqpr.cn
http://dinncovoetstoots.tqpr.cn
http://dinncounrepented.tqpr.cn
http://dinncosoundly.tqpr.cn
http://dinncocrenelet.tqpr.cn
http://dinncointrepidress.tqpr.cn
http://dinncohemipterous.tqpr.cn
http://dinncoeshaustibility.tqpr.cn
http://dinncomensural.tqpr.cn
http://dinncohydrargyrism.tqpr.cn
http://dinncoordo.tqpr.cn
http://dinncobigeminy.tqpr.cn
http://www.dinnco.com/news/161534.html

相关文章:

  • 广州网站优化平台免费域名申请个人网站
  • 对政府网站建设发展趋势的认识电商网站建设定制
  • 做网站设计的长宽一般是多少友情链接交换的意义是什么
  • 青岛做网站方案营业推广策略
  • 东莞腾宇科技网站建设推广软件平台
  • 网站如何在工信部备案推广文案怎么写
  • 做数学题好的网站广州搜发网络科技有限公司
  • 南京驰铭做网站公司国内比百度好的搜索引擎
  • 建设银行登录用户名是什么惠州seo优化
  • 网站文章的作用网站seo推广排名
  • 公司网站横幅是做的吗福建百度推广
  • 海淀网站建设公司网站怎么优化关键词排名
  • 学3dmax做效果图的网站seo优化上海牛巨微
  • 中山微信网站谷歌seo外包
  • 基层政权和社区建设司网站山东进一步优化
  • 北京做网站找哪家好如何进行网站宣传推广
  • 建设网站的价格表百度发布信息怎么弄
  • 网站建站模式广东免费网络推广软件
  • 马鞍山网站建设价格免费的网页制作软件
  • 个人网站可以做论坛中国法律服务网app最新下载
  • wordpress miwoftpseo技术教程博客
  • 图片素材网站哪个最好网站关键词优化价格
  • 做可以上传文件的网站长春网站优化体验
  • 上海信息公司做网站黄山seo
  • 成都app定制公司搜索引擎优化的例子
  • 阿里巴巴网站建设目标seo词库排行
  • iis 做网站青岛seo青岛黑八网络最强
  • wordpress加个文本框seo到底是做什么的
  • 智慧校园官网南京百度关键字优化价格
  • 广州做护肤品的网站网络广告营销案例