如何将事件监听器传递给根组件?

huangapple go评论59阅读模式
英文:

How to pass event listener to the root component?

问题

我正在使用Vue 3,并且我知道在使用组件时,你可以使用v-on指令传递监听器回调函数,也可以使用其简写形式**@**(就像下面的示例中一样)。

<my component @click="myCallback"/>

但是否有一种方法可以将它们传递给应用程序(根组件)?

import { createApp } from 'vue'
// 从单文件组件中导入根组件App。
import MyComponent from './MyComponent.vue'

const app = createApp(MyComponent, {props})
英文:

I am using vue3 and I know that when working with components you can pass listener callbacks using v-on directive or use its shorthand @ (like in the example below).

&lt;my component @click=&quot;myCallback&quot;/&gt;

But is there a way to pass them to the app (root component)?

import { createApp } from &#39;vue&#39;
// import the root component App from a single-file component.
import MyComponent from &#39;./MyComponent.vue&#39;

const app = createApp(MyComponent, {props})

答案1

得分: 2

v-on-* 模板语法被翻译为 h 函数 中的 on* 属性,用于创建虚拟节点。考虑到 MyComponent 发出 foo 事件,代码如下:

const app = createApp(MyComponent, { onFoo() {...} })
英文:

v-on-* template syntax is translated to on* props in h function, which is used to create vnodes. Considering that MyComponent emits foo event, it's:

const app = createApp(MyComponent, { onFoo() {...} })

答案2

得分: 2

你没有将回调传递给组件,而是对它们触发的事件做出反应。如果你想在Vue的“根应用程序”中对某个事件做出反应,只需在根模板中定义v-on

所以,对于你的示例:

如果你将组件放入应用程序模板并将回调定义为应用程序方法,那么当事件发生时将调用你的方法。

应用程序:

<template>
    <my-component @click="myCallback"/>
</template>

methods: {
    myCallback(event) {
        ....
    }
}

示例:

<div id="app" v-cloak>
  <my-comp @click="myCallback"></my-comp>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
英文:

You are not passing callbacks to the components, but reacting to events from them. If you want to react on some event in the Vue root app, then just define v-ons in the root template.

So, for your example:

If you put your component into the app template and define the callback as app method, then your method will be called when the event occurs.

App

&lt;template&gt;
    &lt;my component @click=&quot;myCallback&quot;/&gt;
&lt;/template&gt;

and

methods: {
    myCallback(event) {
    ....
    }
}

Example

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const MyComp = {
  emits: [&#39;click&#39;],
  template: `&lt;button id=&quot;my-button&quot; @click=&quot;$emit(&#39;click&#39;, $event)&quot;&gt;Click me!&lt;/button&gt;`
}

const App = {
 components: { MyComp },
 methods: {
    myCallback(event){
        alert(&#39;Clicked button: &#39; + event.target.id)
    }
  }
}

Vue.createApp(App).mount(&#39;#app&#39;)

<!-- language: lang-css -->

#app { line-height: 1.75; }
[v-cloak] { display: none; }

<!-- language: lang-html -->

&lt;div id=&quot;app&quot; v-cloak&gt;
  &lt;my-comp @click=&quot;myCallback&quot;&gt;&lt;/my-comp&gt; 
&lt;/div&gt;
&lt;script src=&quot;https://unpkg.com/vue@3/dist/vue.global.prod.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月4日 18:21:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75928222.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定