Vue3 中 render函数中怎么接收子组件$emit发送的事件

Vue3.x 中 render 函数中怎么接收子组件$emit发送的事件

子组件

<template>
    <div class='action-btns'>
        <q-btn v-if='start' @click.stop='$emit("onStart")'/>
    </div>
</template>

父组件 render 函数

render(h, { row }) {
    return h(TableActionButtons, {
        //子组件$emit传递函数
        onStart($event) {
            console.log(row.id);
        },
    });
}

没有效果,接收不了。。。

我知道2.0中是这样的,但是3.0不能这样写了

render(h, { row }) {
    return h(TableActionButtons, {
        on:{
           // 子组件$emit传递函数
           onStart($event) {
             console.log(row.id);
           },
        }
    });
}

请问一下,Vue3.0 怎样在父组件 render 中接收 onStart 方法,试了很多种方法都没用,谢谢解答!

阅读 12.1k
3 个回答
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app"></div>
    <script src="https://cdn.staticfile.org/vue/3.1.5/vue.global.js"></script>
    <script>
        const Test = Vue.defineComponent({
            template: `<button @click="$emit('onStart', 'child')">emit start</button>`
        })
        Vue.createApp({
            render() {
                return Vue.h(Test, {
                    onOnStart: arg => console.log('app', arg)
                })
            }
        }).mount('#app')
    </script>
</body>
</html>

emit的是onStart,那监听得是onOnStart。你看你改哪一头

父组件

<Search @searchData="searchData" :quaryParams="quaryParams"/>.

父组件的写法和vue还是一样的,只是子组件需要作一些改变

子组件

<script lang="ts">
import { defineComponent } from 'vue';
interface GetUserListParams {
    pageNum: number;
    pageSize: number;
    roleName: string;
}
export default defineComponent({
    name: 'Search',
    props: {
        quaryParams: {
            type: Object as PropType<GetUserListParams> ,
            default: () = > ({
                pageNum: 1,
                pageSize: 10,
                roleName: ''
            })
        }
    },
    emits: ['searchData'],//需要声明emits
    setup(_props, context) {
    
      const onSubmit = () => {
        context.emit('searchData', "我是子节点传递给父节点的值");
      }
      
      return {
        getData
      }
    }
});
</script>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题