简体   繁体   English

将键盘事件传递给子组件

[英]Pass keyboard events to child component

See this simple modal component:查看这个简单的模态组件:

Vue.component('modal-content', {
  template: `
      <div class="modal">
         ...
      </div>
  `,

  props: ['title'],

});

How can I control keyboard press from the components that use it (parents)?如何从使用它的组件(父母)控制键盘按下?

If I use @keyup.esc event then it does not have any effect:如果我使用 @keyup.esc 事件,那么它没有任何效果:

<portal to="modal-wrap">
  <modal-content @keyup.esc="doSomething">
  </modal-content>
</portal> 

If I put @keyup.esc in the modal-content component on the DIV then it works, but that's no use to me because I need to call the doSomething function which exists in the parent component如果我将 @keyup.esc 放在 DIV 的 modal-content 组件中,那么它可以工作,但这对我来说没用,因为我需要调用父组件中存在的doSomething function

The reason your code is not working is because your you are trying to listen to a native browser event - which is not emitted by Vue, but by the DOM itself.您的代码不起作用的原因是因为您正在尝试侦听本机浏览器事件 - 这不是由 Vue 发出的,而是由 DOM 本身发出的。

To react to these type of events you have to attach your listener to an actual HTMLElement - not a vue component.要对这些类型的事件做出反应,您必须将侦听器附加到实际的HTMLElement - 而不是 vue 组件。 Since this is a common issue, vue provides a simple modifier that automatically attaches the eventlistener to the underlying HTMLElement : the .native modifier.由于这是一个常见问题,vue 提供了一个简单的修饰符,可以自动将事件监听器附加到底层HTMLElement.native修饰符。

Try it like this像这样试试

<portal to="modal-wrap">
  <modal-content @keyup.esc.native="doSomething">
  </modal-content>
</portal> 

You can find further information in vues documentation您可以在 vues文档中找到更多信息

You could do something like this in your doSomething function:你可以在你的 doSomething function 中做这样的事情:

onClickButton (event) {
      this.$emit('clicked', 'someValue')
}

And in you parent component do this:在你的父组件中这样做:

<template>
   <parent-component @clicked="handleClick"></parent-component>
</template>

For more info check this:有关更多信息,请查看:

https://v2.vuejs.org/v2/guide/components.html#Listening-to-Child-Components-Events https://v2.vuejs.org/v2/guide/components.html#Listening-to-Child-Components-Events

And this:和这个:

https://medium.com/js-dojo/component-communication-in-vue-js-ca8b591d7efa https://medium.com/js-dojo/component-communication-in-vue-js-ca8b591d7efa

Use refs ( Accessing Child Component Instances & Child Elements )使用refs访问子组件实例和子元素

<modal-content @keyup.esc="doSomething" ref="modal">

then inside the doSomething method:然后在 doSomething 方法中:

this.$refs.modal.close();

presuming you have a close() method in the modal-content component.假设您在 modal-content 组件中有一个close()方法。


You could also pass a prop , like you are doing with title :你也可以传递一个prop ,就像你对title所做的那样:

<modal-content @keyup.esc="modalIsOpen = false":modalIsOpen="modalIsOpen">

Vue.component('modal-content', {
  template: `
      <div class="modal" :class="{show: modalIsOpen}">
         ...
      </div>
  `,
  props: ['title' 'modalIsOpen'],
  //...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM