search
HomeWeb Front-endJS TutorialExamples of vue development skills worth collecting

Examples of vue development skills worth collecting

Jan 25, 2018 pm 02:53 PM
ExampledevelopSkill

In this article, we will elaborate on some useful development skills of vue from many aspects. They are worth collecting. I hope they can help everyone.

1. The clever use of placeholder and computed

Form development is definitely an indispensable part of daily development, but design drawings often have form default values, such as:
Examples of vue development skills worth collecting

The demand point of the demander is : display the default value when no value is entered, and display the input when the value is entered. value.

Usually you can think of using placeholder to solve this problem, and usually use v-model to bind the value in data. Then, the value of data is set to the default value as empty

//script
data(){
    return {
        index:0,
        name:''
    }
}
//template
<input>
<input>

The above effect is that the value of the placeholder of the first input cannot be displayed, and the value of index is displayed: 0 , does not meet the requirements
The second type can display the value of the placeholder, and the requirements are met.

But for some complex requirements, for example, allowing the user to select a city name (city) and a country name (country), and finally display them in a variable (countryAndCity), in this case computed

//template
<input>
<input>
<input>

//script
data(){
    return {
        city:'',
        country:''
    }
},
computed:{
    countryAndCity () {
        let str = ''
        if(this.city && this.country){
            str = `${this.city}+${this.country}`
        }
        return str
    }
}

You need to make a judgment above. The result will be displayed when city and country have values. Otherwise, the value of placeholder will be displayed.

2. Design of single-select and multi-select selections

Such as radio-select and multi-select buttons designed by designers
Radio buttons are relatively simple

//template
  • {{item}}
  • //script data(){     return {         currentIndex:0,         list:['aa','bb','cc','dd']     } }, methods:{     select(index){         this.currentIndex = index     } }

    The above is very simple. You can understand it after just a look. This is a single-select situation. If multi-select is the case, then you need to change your thinking.

    First change the data format.

    data(){
        return {
            list:[
            {text:'aa',isActive:false},
            {text:'bb',isActive:false}
            {text:'cc',isActive:false}'
            ]
        }
    },
    methods:{
        select(index){
            this.list[index].isActive = !this.list[index].isActive
        }
    }

    Then the template will become like this

    
    
  • {{item.text}}
  • 3. Use of dynamic components and asynchronous components

    Dynamic components are generally rarely used, but you need to dynamically introduce components It's really useful when using it. It is the core of the component configuration system we have done before. I use a dynamic component loop, then use is to get the component name, and use props to get the custom props of each component

    <components></components>
    
    componentList:[{ name:'index',props:{title:'title'}}]

    4. Server-side rendering of created and mounted

    created and mounted The window object exists during client rendering, so it can be operated directly.
    But during server-side rendering, the windows of both of them do not exist, so a judgment must be added in front of all logic

    if(typeof window !== 'object') return ;

    5.The wonderful use of this.$emit

    Based on component thinking, many times we will split a page into several components, and then extract some common components, such as the dialog pop-up component. Its opening and closing are based on the data of the referenced component page. Determined by a value,

    //app.vue
    <dialog></dialog>
    
    data(){
        return {
            isDialog:false
        }
    }
    methods:{
        showDialog(){
            this.isDialog = true
        }
    }

    But the close button is usually written inside the dialog component, that is to say, there is no such button that can be clicked on the reference component page,
    So, you can put it in the dialog The signal of the click time is passed out, the reference component page receives the signal, and then controls the shutdown

    //dialog.vue
     
    <p> 点击关闭 </p>
    
    methods:{
        close() {
            this.$emit('close')
        }
    }    
    
    //app.vue
    <dialog></dialog>
    
    data(){
        return {
            isDialog:false
        }
    }
    methods:{
        showDialog(){
            this.isDialog = true
        },
        closeDialog(){
            this.isDialog = false
        }
    }

    The general idea is to put the actual closing operation in isDialog page for easy operation.
    There will be a public component writing method that does not reference this way and is referenced directly in the methods method. Please stay tuned

    6.css scoped

    css in vue can be used The scoped key is used to limit the scope of css.

    <style>...</style>

    This is used every day, because there is no need to consider the overlapping of class names, plus the use of css processors such as sass, less, stylus, postcss, etc. The effect is simply overwhelming.
    But if you want to change the css style of the body element, but don’t want to change the public layout template. Then you can write two style tags

    <style> body{...} </style>
    <style> .. .</style>

    in a vue file. Related recommendations:

    Vue implementation of splitting mobile phone numbers in the number input box example tutorial

    detailed explanation of vue syntax splicing strings

    #vue detailed explanation of using eventbus to pass values ​​between components

    Detailed explanation of vue transition animation

    Vue implements a simple example of the 60-second countdown function of the verification code

    The above is the detailed content of Examples of vue development skills worth collecting. For more information, please follow other related articles on the PHP Chinese website!

    Statement
    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
    Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

    The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

    The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

    JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

    Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

    JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

    The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

    The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

    Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

    Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

    Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

    Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

    JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

    JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

    C   and JavaScript: The Connection ExplainedC and JavaScript: The Connection ExplainedApr 23, 2025 am 12:07 AM

    C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

    See all articles

    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

    SAP NetWeaver Server Adapter for Eclipse

    SAP NetWeaver Server Adapter for Eclipse

    Integrate Eclipse with SAP NetWeaver application server.

    Atom editor mac version download

    Atom editor mac version download

    The most popular open source editor

    SecLists

    SecLists

    SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    EditPlus Chinese cracked version

    EditPlus Chinese cracked version

    Small size, syntax highlighting, does not support code prompt function