Home  >  Article  >  Web Front-end  >  Examples of vue development skills worth collecting

Examples of vue development skills worth collecting

小云云
小云云Original
2018-01-25 14:53:422334browse

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 type="number" placeholder="默认值index" v-model="index"/>
<input type="text" placeholder="默认值name" v-model="name"/>

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 type="text" placeholder="城市" v-model="city"/>
<input type="text" placeholder="国家" v-model="country"/>
<input type="text" placeholder="国家+城市" v-model="countryAndCity"/>

//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
<li v-for="item, index in list" :class="{"active":currentIndex === index}" @click="select(index)">{{item}}</li>

//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

<li v-for="(item, index) in list" :class="{"active":item.isActive}" @click="select(index)">{{item.text}}</li>

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 :is="item.name" v-for="item, index in componentList" :props="item.props"></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 v-if="isDialog"></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 @click="close"> 点击关闭 </p>

methods:{
    close() {
        this.$emit('close')
    }
}    

//app.vue
<dialog v-if="isDialog" @close="closeDialog"></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 scoped>...</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 scoped> .. .</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