PrefaceThis article records some commonly used tool functions in Vue projects. For unified management of tool functions, these functions are uniformly placed in the utils folder under the src directory 1. Custom focus command 1. Method 1mouted cycle, ref+querySelector gets the input tag and calls focus() 2. Method 2Custom directives (local) directives:fofo(inserted), defined and used on the tag, v-fofo 3. Method 3Global custom instructions, recommended (high reusability). Just import it in main.js and use it. The code is as follows (example): import Vue from 'vue' Vue.directive("fofo",{ inserted(el) { // Determine the name of the element obtained if (el.nodeName === 'INPUT' || el.nodeName === 'TEXTAREA') { el.focus() } else { // Try to get the inner layer el.querySelector('input').focus() } } }) 2. Input box anti-shake 1. DemandWhen the user enters content in the input box, in order to get the user's input content and feed it back to the server, it is necessary to monitor the input event of the input box. However, when the value of the input box changes, an Ajax request is sent immediately, which will cause some unnecessary Ajax requests. When the user stops typing and waits for a certain period of time, the request is sent to the background, which can reduce some unnecessary requests. 2. IdeasWhen the user starts typing, a timer is started. If the user does not enter any content again after the timer ends, an Ajax request is sent to the background. If the user inputs again within the specified time, the previous timer will be cleared and the timer will be restarted. 3. Code implementationHere is an example to demonstrate that after understanding the implementation principle, the code can be extracted. The code is as follows (example): <template> <div> <input type="text" v-model="kw" @input="inputFn"/> </div> </template> <script> export default{ data(){ return { kw:'', timer:null } }, methods:{ inputFn(){ clearTimeout(this.timer) this.timer = setTimeout(() => { if(this.kw === '') return // Here you can send an Ajax request to get the search association list returned by the background according to the keywords entered by the user console.log(this.kw) }, 1000) // When the user stops inputting content for one second, the logic in the timer will be executed. If the user writes content again within one second, the timer will be restarted. } } } </script> 3. Keyword highlighting 1. DemandWhen a user searches for a keyword in the input box, the color of the keyword in the displayed association list will change, allowing the user to see the desired results more intuitively. 2. IdeasEncapsulate a lightFn function, which receives two arguments, the first is the string to be modified, and the second is the keyword to be matched 3. Code DemonstrationThe code is as follows (example): export const lightFn = (str, targetStr) => { // Ignore case and match globally const reg = new RegExp(targetStr, 'ig') return str.replace(reg, match => { return `<span style="color:red">${match}</span>` }) } IV. Format the time stored in Excel table 1. DemandConvert the time stored in the Excel table to be imported from the Excel format to the format used for storage. 2. Code DemonstrationThis code is quoted from Lan Yuxi. Thanks to this big guy, I will collect it here~ The code is as follows (example): export function formatExcelDate(numb, format = '/') { const time = new Date((numb - 25567) * 24 * 3600000 - 5 * 60 * 1000 - 43 * 1000 - 24 * 3600000 - 8 * 3600000) time.setYear(time.getFullYear()) const year = time.getFullYear() + '' const month = time.getMonth() + 1 + '' const date = time.getDate() + '' if (format && format.length === 1) { return year + format + month + format + date } return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date) } SummarizeThis is the end of this article about the commonly used tool functions in Vue projects. For more relevant Vue commonly used tool functions, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Nginx/Httpd reverse proxy tomcat configuration tutorial
>>: Using System.Drawing.Common in Linux/Docker
Table of contents 1. Listening for events 2. Pass...
The hyperlink a tag represents a link point and i...
If you want to wrap the text in the textarea input...
Import the data exported from the Oracle database...
Must read before operation: Note: If you want to ...
Source code preview: https://github.com/jdf2e/nut...
Note: When writing the docker-compose.yml file, a...
Table of contents Mistake 1: Too many columns of ...
The installation of MySQL 8.0.12 took two days an...
Preface The file system is responsible for organi...
Rendering principle In the form element, there is...
Build the image Earlier we used various images fo...
background Two network cards are configured for t...
This article does not have any quibbles, it is jus...
MySQL is the most popular relational database man...