简体   繁体   English

在自定义输入字段上使用 keyup enter - Vue JS

[英]Use keyup enter on custom input field - Vue JS

I have to use keypu.enter in a custom input component on Vue.我必须在 Vue 的自定义输入组件中使用keypu.enter I want that the component <input /> can execute a function hosted in parent component after a enter key event during typing我希望组件<input />可以在键入期间的输入键事件后执行托管在父组件中的 function
This is the component code这是组件代码

<input v-on:keyup.enter="$emit('keyup')"/>

And there are the main page还有主页

<template>
  <se-input @keyup="function()"/>
</template>

<script>
import inputField from '../components/inputfield.vue'

export default {
  name: 'inputField',
  components: {
      'custom-input': inputField
    },
  },
  methods: {
    function () {
      // Function
    }
  }
}
</script>

Try to pass value to your custom event, and in parent component listen to that event:尝试将值传递给您的自定义事件,并在父组件中监听该事件:

 Vue.component('seInput', { template: ` <div class=""> <input v-on:keyup.enter="$emit('keyup', $event.target.value)"/> </div> ` }) new Vue({ el: '#demo', data() { return { inputValue: null } }, methods: { handleInput(val) { this.inputValue = val } } })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="demo"> <h3>{{ inputValue }}</h3> <se-input @keyup="handleInput"/> </div>

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

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