1. v-text
v-text is mainly used to update textContent, which can be equivalent to JS's text attribute.
<span v-text="msg"></span>
These two are equivalent:
<span>{{msg}}</span>
2. v-html
The double braces interpret the data as plain text, not HTML. To output real HTML, you can use the v-html directive. It is equivalent to JS's innerHtml property.
<div v-html="rawHtml"></div>
The content of this div will be replaced with the attribute value rawHtml and will be rendered directly as HTML.
3. v-pre
v-pre is mainly used to skip the compilation process of this element and its child elements. Can be used to display the original Mustache tag. Skip a large number of nodes without instructions to speed up compilation.
<div > <span v-pre>{{message}}</span> //This statement is not compiled <span>{{message}}</span> </div>
Finally, only the content of the second span is displayed
4. v-cloak
This directive is used to keep on the element until the associated instance is finished.
<div v-cloak> <div> {{message}} </div> </div> <script type="text/javascript"> new Vue({ el:'#app', data:{ message:'hello world' } }) </script>
When the page is loading, it will flash, first display:
<div> {{message}} </div>
Then it compiles to:
<div> hello world! </div>
5. v-once
The instance associated with v-once will only be rendered once. After re-rendering, extremely all child nodes of the instance will be skipped as static content, which can be used to optimize update performance.
<span v-once>This will never change:{{msg}}</span> //Single element<div v-once>//There are child elements <h1>comment</h1> <p>{{msg}}</p> </div> <my-component v-once:comment="msg"></my-component> //Component<ul> <li v-for="i in list">{{i}}</li> </ul>
In the above example, even if msg and list change, they will not be re-rendered.
6. v-if
v-if can implement conditional rendering, and Vue will render elements based on the true or false conditions of the expression value.
<a v-if="ok">yes</a>
If the attribute value ok is true, it will be displayed. Otherwise, this element will not be rendered.
7. v-else
v-else is used with v-if, it must be followed immediately after v-if or v-else-if, otherwise it will not work.
<a v-if="ok">yes</a> <a v-else>No</a>
8. v-else-if
v-else-if acts as the else-if block of v-if and can be used in chain multiple times. It can more conveniently implement switch statements.
<div v-if="type==='A'"> A </div> <div v-if="type==='B'"> B </div> <div v-if="type==='C'"> C </div> <div v-else> Not A,B,C </div>
9. v-show
<h1 v-show="ok">hello world</h1>
It is also used to display elements according to conditions. Unlike v-if, if the value of v-if is false, this element is destroyed and not in the dom. But the element of v-show will always be rendered and saved in the dom, it simply switches the display property of css.
Note: v-if has higher switching overhead
v-show has higher initial rendering overhead.
Therefore, if you want to switch very frequently, it is better to use v-show; if the conditions are unlikely to change at runtime, v-if is better
10. v-for
Use the v-for directive to render according to the traversal array
There are two traversal forms below
<div v-for="(item,index) in items"></div> //Use in, index is an optional parameter that represents the index of the current item<div v-for="item of items"></div> //useof
Here is an example, and in v-for, you have full access to the parent scope attribute.
<ul > <li v-for="item in items"> {{parent}}-{{}} </li> </ul> <script type="text/javascript"> var example = new Vue({ el:'#app', data:{ parent:'Parent scope' items:[ {text:'Text 1'}, {text:'Text 2'} ] } }) </script>
Will be rendered as:
<ul > <li>Parent scope-text1</li> <li>Parent scope-text2</li> </ul>
Note: When v-for and v-if are on the same node, v-for has higher priority than v-if. This means that v-if will run in each v-for loop
11. v-bind
v-bind is used to dynamically bind one or more features. When there are no parameters, you can bind to an object containing key-value pairs. Commonly used to dynamically bind class and style. and href, etc.
Abbreviated as a colon [ : ]
<1>Object Syntax:
//Example of class switching<div > <!--whendataThe definition in itisActiveequaltruehour,is-activeThis class will be added to work--> <!--whendataThe definition in ithasErrorequaltruehour,text-dangerThis class will be added to work--> <div :class="{'is-active':isActive, 'text-danger':hasError}"></div> </div> <script> var app = new Vue({ el: '#app', data: { isActive: true, hasError: false } }) </script>
Rendering result:
<!--becausehasError: false,sotext-dangerNot rendered--> <div class = "is-active"></div>
<2>Array Syntax
<div > <!--Array Syntax:errorClassexistdataThe corresponding class will definitely be added--> <!--is-activeIt's object syntax,according toactiveClassThe corresponding value determines whether to add it--> <p :class="[{'is-active':activeClass},errorClass]">12345</p> </div> <script> var app = new Vue({ el: '#app', data: { activeClass: false, errorClass: 'text-danger' } }) </script>
Rendering result:
<!--becauseactiveClass: false,sois-activeNot rendered--> <p class = "text-danger"></p>
<3>Bind data objects directly
<div > <!--existvueExampledataDefined inclassObjectObject,这个Object里面是所有类名及其真值--> <!--When the value of the class inside istrueWill be rendered when--> <div :class="classObject">12345</div> </div> <script> var app = new Vue({ el: '#app', data: { classObject:{ 'is-active': false, 'text-danger':true } } }) </script>
Rendering result:
<!--because'is-active': false,sois-activeNot rendered--> <div class = "text-danger"></div>
12. v-model
This directive is used to create two-way data bindings on the form.
v-model ignores the initial values of the value, checked, and selected properties of all form elements. Because it selects Vue instance data as the specific value.
<div > <input v-model="somebody"> <p>hello {{somebody}}</p> </div> <script> var app = new Vue({ el: '#app', data: { somebody:'Xiao Ming' } }) </script>
In this example, enter another name directly into the browser input, and the content of the p below will change directly. This is two-way data binding.
v-model modifier
<1> .lazy
By default, v-model synchronizes the value and data of the input box. This modifier can be used to resynchronize the change event.
<input ="msg">
<2> .number
Automatically convert user input values into numeric types
<input ="msg">
<3> .trim
Automatically filter the beginning and end spaces entered by user
<input ="msg">
13. v-on
v-on is mainly used to listen for dom events in order to execute some code blocks. An expression can be a method name.
Abbreviation is: [ @ 】
<div > <button @click="consoleLog"></button> </div> <script> var app = new Vue({ el: '#app', methods:{ consoleLog:function (event) { (1) } } }) </script>
Event modifier
.stop Prevent the event from continuing to spread
The .prevent event no longer reloads the page
.capture uses event capture mode, that is, events triggered by the element itself are processed here first, and then handed over to the internal element for processing.
.self triggers the processing function only when it is the current element itself
The .once event will only fire once
.passive tells the browser that you don't want to block events default behavior
<!-- Prevent click events from continuing to spread --> <a v-on:="doThis"></a> <!-- Submit event no longer reloads the page --> <form v-on:="onSubmit"></form> <!-- Modifiers can be concatenated --> <a v-on:="doThat"></a> <!-- Only modifiers --> <form v-on:></form> <!-- Use event capture mode when adding event listeners --> <!-- That is, the events triggered by the element itself are processed here first,Then it is handed over to the internal element for processing --> <div v-on:="doThis">...</div> <!-- Just as The processing function is triggered when the current element itself --> <!-- That is, events are not triggered from internal elements --> <div v-on:="doThat">...</div> <!-- Click event will only trigger once --> <a v-on:="doThis"></a> <!-- Default behavior of scrolling events (That is, scrolling behavior) Will trigger immediately --> <!-- And won't wait `onScroll` Finish --> <!-- This includes `()` The situation --> <div v-on:="onScroll">...</div>
When using modifiers, the order is important; the corresponding code will be generated in the same order. Therefore, using v-on: will block all clicks, and v-on: will only block clicks on the element itself.