Home Web Front-end JS Tutorial How vue implements page keyboard events (with code)

How vue implements page keyboard events (with code)

Aug 17, 2018 pm 03:33 PM
Keyboard events

The content of this article is about how vue implements page keyboard events (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Our company's development project uses vue elementUI. When making the login page, when clicking the enter key, it needs to achieve the same function as clicking the login button, so I searched on Baidu, so I went through Baidu. After that, I added @keyup.enter.native="submitForm('loginData')" directly on the click button. I was so happy that after saving it, I went to the login page and clicked the enter key, but it didn't work. Then Baidu discovered that if you use element to directly bind keyboard events to buttons or el-input, it is useless because you need to get focus first. Solving the problem is the first priority, followed by Baidu. Found the correct binding method: insert the following code into the created hook of vue and it will be ok

created(){
            var _self = this;
            document.onkeydown = function(e){
                var key = window.event.keyCode;
                if(key == 13){
                    _self.submitForm('loginData');
                }
            }
        }
Copy after login

By the way, I will also post my login code:

methods: {
            submitForm(formName) {
                var _self = this;
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                       getInfo.post('api-token-auth/',{username:_self.loginData.userCount,password:_self.loginData.password}).then(function(data){
                            if(data.data.code == 0){
                                let jwtSession = 'JWT'+' '+data.data.token;
                                localStorage.setItem("checkSession", jwtSession);
                                localStorage.setItem("userInfo", data.data.userinfo.username);
                                localStorage.setItem("routes",JSON.stringify(data.data.userinfo.permissions))
                                // 路由权限过滤
                                var menuData = JSON.parse(localStorage.getItem('routes'));
                                var localRouter = _self.$router.options.routes
                                for(let i = 0;i<menuData.length;i++){
                                  for(let q = 0;q<localRouter.length;q++){
                                    if(menuData[i].codename == localRouter[q].path.replace(/\//,"")){
                                      localRouter[q].hidden = false;
                                    } 
                                  }
                                }
                                _self.$router.addRoutes(localRouter)
                                _self.$router.push({ path: &#39;/ops_menu_sever_manage&#39;});
                            }
                            else{
                                _self.$message({
                                    message: data.data.msg,
                                    type: &#39;warning&#39;
                                });
                                // _self.$router.push({ path: &#39;/login&#39;});
                            }
                            
                       });
                    } else {
                        console.log("验证没通过!")
                    }
                });
            },
            
        },
Copy after login

This is it , you're done.

Related recommendations:

How vue configures keyboard events globally

## The keyboard enter event is bound multiple times to one page_html /css_WEB-ITnose

The above is the detailed content of How vue implements page keyboard events (with code). For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Learn to use Vue's v-on directive to handle keyboard shortcut events Learn to use Vue's v-on directive to handle keyboard shortcut events Sep 15, 2023 am 11:01 AM

Learn to use Vue's v-on directive to handle keyboard shortcut events. In Vue, we can use the v-on directive to listen for element events, including mouse events, keyboard events, etc. This article will introduce how to use the v-on directive to handle keyboard shortcut events and provide specific code examples. First, you need to define a method in the Vue instance to handle shortcut key events. For example, we can add a method named handleShortcut to methods: methods:{

Basic tutorial for learning Pygame: Quick introduction to game development Basic tutorial for learning Pygame: Quick introduction to game development Feb 19, 2024 am 08:51 AM

Pygame installation tutorial: Quickly master the basics of game development, specific code examples are required Introduction: In the field of game development, Pygame is a very popular Python library. It provides developers with rich features and easy-to-use interfaces, allowing them to quickly develop high-quality games. This article will introduce you in detail how to install Pygame and provide some specific code examples to help you quickly master the basics of game development. 1. Installation of Pygame Install Python and start installing Pyga

Event Types in JavaScript: Common Keyboard and Mouse Events Event Types in JavaScript: Common Keyboard and Mouse Events Sep 03, 2023 am 09:33 AM

JavaScript provides a wide range of events that allow you to interact with and respond to user actions on web pages. Among these events, keyboard and mouse events are the most commonly used. In this article, we'll look at the different types of keyboard and mouse events in JavaScript and see examples of how to use them. Keyboard events Keyboard events occur when a user interacts with the keyboard, such as pressing a key, releasing a key, or typing a character. Keyboard events allow us to do some cool things, such as checking if the user entered something correctly into a form, or that something happens when a specific key is pressed. It's as if the website is listening to the keys you press and reacts accordingly. Keyboard events are divided into three types: keydown event

Pygame Installation Guide: An easy-to-understand introductory tutorial Pygame Installation Guide: An easy-to-understand introductory tutorial Feb 20, 2024 pm 12:39 PM

Pygame Installation Tutorial: A simple and easy-to-understand getting started guide, requiring specific code examples Introduction: Pygame is a very popular Python library for developing 2D games. It provides rich functions and easy-to-use interfaces, making game development easier and more interesting. This article will introduce you to the installation process of Pygame and provide specific code examples to help beginners get started quickly. 1. Install Python and Pygame. Download Python and Pygame: First you need to install Python.

How to use Golang to create an efficient game development framework How to use Golang to create an efficient game development framework Mar 06, 2024 pm 06:15 PM

As a popular and efficient programming language in the industry, Golang is also widely used in the field of game development. This article will introduce how to use Golang to create an efficient game development framework and provide specific code examples. We will take a simple 2D game as an example to explain. Part One: Game Engine Construction First, we need to build a simple game engine, including game loop, graphics rendering and other functions. The following is a simple game engine framework: packageenginei

The pros and cons of developing desktop applications in Golang The pros and cons of developing desktop applications in Golang Apr 08, 2024 pm 03:42 PM

The advantages of using Go language in cross-platform desktop development include: cross-platform, efficiency, concurrency, and powerful standard library. The disadvantages are: GUI limitations, weak native IDE support, and high resource consumption. If you consider developing a cross-platform text editor, you can use the Go standard library to handle file I/O and text formatting, and use third-party libraries to create cross-platform interfaces.

How to solve Vue error: Unable to use v-on correctly to monitor keyboard events How to solve Vue error: Unable to use v-on correctly to monitor keyboard events Aug 17, 2023 pm 10:27 PM

How to solve Vue error: Unable to use v-on to listen to keyboard events correctly. As a popular front-end framework, Vue.js can help us build efficient, flexible and maintainable web applications. Among them, Vue provides the v-on instruction to monitor DOM events to facilitate us to handle user operations. However, when using v-on to monitor keyboard events, we sometimes encounter some errors that prevent us from using this function correctly. This article will walk you through this problem and provide some code examples. Check Vue version

Practical applications of event bubbling and applicable event types Practical applications of event bubbling and applicable event types Feb 18, 2024 pm 04:19 PM

Application scenarios of event bubbling and the types of events it supports. Event bubbling means that when an event on an element is triggered, the event will be passed to the parent element of the element, and then to the ancestor element of the element until it is passed to the root node of the document. It is an important mechanism of the event model and has a wide range of application scenarios. This article will introduce the application scenarios of event bubbling and explore the types of events it supports. 1. Application scenarios Event bubbling has a wide range of application scenarios in web development. Here are several common application scenarios. form validation in form

See all articles