Home > Article > Web Front-end > Examples of vue development skills worth collecting
Form development is definitely an indispensable part of daily development, but design drawings often have form default values, such as:
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.
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>
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'}}]
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 ;
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
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!