vue3组合式api中,子组件给父组件传递事件的写法

子组件

export default defineComponent({
  components: {...},
  props: {...},
  // 定义抛出的事件名称
  emits: ["player-play"],
  setup(props,context) {
  	//抛出
    const play = () => {
      ...
      context.emit('player-play', 附带的数据);
      ...
    }
  }
})

父组件

  <a-map-topo-play :visible="amapTopoPlay" @player-play="playerPlay"/>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3组件可以通过props接收父组件传递的值,也可以通过$emit方法向父组件发送事件。具体操作如下: 1. 父组件传递值给组件: 父组件使用组件时,可以在组件标签上使用属性绑定的方式,将父组件的数据传递组件: ```html <template> <div> <ChildComponent :msg="parentMsg" /> </div> </template> <script> import ChildComponent from './ChildComponent.vue' export default { name: 'ParentComponent', components: { ChildComponent }, data() { return { parentMsg: 'Hello from parent!' } } } </script> ``` 组件通过props接收父组件传递的值: ```html <template> <div> {{ msg }} </div> </template> <script> export default { name: 'ChildComponent', props: { msg: String } } </script> ``` 2. 组件向父组件发送事件组件通过$emit方法触发一个自定义事件,父组件通过在组件标签上使用v-on绑定自定义事件的方式,监听组件触发的事件并处理: ```html <template> <div> <button @click="sendMsg">发送消息到父组件</button> </div> </template> <script> export default { name: 'ChildComponent', methods: { sendMsg() { this.$emit('send-msg', 'Hello from child!') } } } </script> ``` 父组件监听组件触发的事件,并处理: ```html <template> <div> <ChildComponent @send-msg="handleMsg" /> </div> </template> <script> import ChildComponent from './ChildComponent.vue' export default { name: 'ParentComponent', components: { ChildComponent }, methods: { handleMsg(msg) { console.log(msg) } } } </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值