Home > Web Front-end > JS Tutorial > body text

Detailed explanation of Vue key modifier processing event steps

php中世界最好的语言
Release: 2018-05-23 15:49:52
Original
1826 people have browsed it

This time I will bring you a detailed explanation of the steps of Vue key modifier processing event , what are the precautions for Vue key modifier processing event, the following is a practical case, let's take a look one time.

Key modifier

When developing on the PC side, we often encounter similar requirements, such as submitting a form when the user presses the enter key without using key modifiers. When using a character, we may listen to keyboard events and judge based on the value of keyCode

Vue adds key modifiers and system modifiers to handle similar events

/** 提交表单 */
<template>
  <p class="demo">
    电话号码:
    <input type="text" placeholder="请输入电话号码" v-model="phone" @keyup.13="handleSubmit" />
  </p>
</template>
<script>
export default {
  data () {
    return {
      phone: '' // 电话号码
    }
  },
  methods: {
    // TODO 提交电话号码
    handleSubmit () { alert(this.phone) }
  }
}
</script>
Copy after login

Remember all keyCode Values ​​are difficult, so Vue provides aliases for commonly used keys

<input type="text" placeholder="请输入电话号码" v-model="phone" @keyup.enter="handleSubmit" />
Copy after login

Common button aliases

enter tab delete esc space up down left right

If these aliases cannot meet the needs, you can customize the key modifier alias through global config.keyCodes Object

Vue.config.keyCodes.x = 88

You can also convert the key name exposed by keyboardEvent.key to kebab-case as a modifier. The following two modifiers can trigger the handleSubmit event

<input type="text" placeholder="请输入电话号码" v-model="phone" @keyup.right="handleSubmit" />
<input type="text" placeholder="请输入电话号码" v-model="phone" @keyup.arrow-right="handleSubmit" />
Copy after login

System Modifier Key

Sometimes we need to trigger an event together with the system modifier key. It should be noted here that pressing the system modifier key alone will not trigger the corresponding event

The system modifier keys include the ctrl alt shift meta key. For different keyboards, the four system modifier keys correspond to different ones. For the mac system keyboard, the meta key corresponds to the command key, and in the windows system keyboard it corresponds to the ⊞ key

In the following example, the handleSubmit event will be triggered when the control and v keys work together

<input type="text" placeholder="请输入电话号码" v-model="phone" @keyup.ctrl.v="handleSubmit"/>
Copy after login

Sometimes we need to accurately match the key combination before triggering the corresponding event. In the following example, when And only when the control and v keys work together, the handleSubmit event will be triggered

<input type="text" placeholder="请输入电话号码" v-model="phone" @keyup.ctrl.v.exact="handleSubmit"/>
Copy after login

I believe that after reading the case in this article, you have mastered the method. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

How to use Angular template to drive forms

How to use Koa in Node.js to implement user authentication

The above is the detailed content of Detailed explanation of Vue key modifier processing event steps. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template