How to emit an event from Vue.js Functional component?

后端 未结 3 973
悲哀的现实
悲哀的现实 2021-02-05 08:30

As the title of the question, this context is not available in the functional component. So if I have to emit an event, how can I do that?

For example in be

3条回答
  •  天涯浪人
    2021-02-05 09:10

    Child Component

    
    

    Parent Component

    
    

    See it in action on codesandbox

    Do you want to emit the event from the vue instance?

    export default {
      functional: true,
      render(createElement, { listeners }) {
        return createElement(
          "button",
          {
            on: {
              click: event => {
                const emit_event = listeners.event_from_child;
                emit_event("Hello World!Is this the message we excpected? :/");
              }
            }
          },
          "Pass event to parent"
        );
      }
    };
    

    See it also a sandbox example here

提交回复
热议问题