Home  >  Article  >  Web Front-end  >  Summary of commonly used instructions in Vue

Summary of commonly used instructions in Vue

不言
不言Original
2018-07-26 12:53:231446browse

The content shared with you in this article is a summary of commonly used instructions in Vue. The content is very detailed. Next, let’s take a look at the specific content. I hope it can help friends in need.

1 Commonly used commands

  • v-if command

  • v-show command

  • v-else instruction

  • v-for instruction

  • v-bind instruction

  • v-model

  • v-on command

  • v-text command

1.1 v-if is a conditional rendering instruction, which deletes and inserts elements based on the true or false expression. Its basic syntax is as follows:

v-if="expression"

expression is an expression that returns a bool value. Expression The formula can be a bool attribute or an operation formula that returns bool. For example:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
    </head>
    <body>
        <div id="app">
            <h1>Hello, Vue.js!</h1>
            <h1 v-if="yes">Yes!</h1>
            <h1 v-if="no">No!</h1>
            <h1 v-if="age >= 25">Age: {{ age }}</h1>
            <h1 v-if="name.indexOf(&#39;jack&#39;) >= 0">Name: {{ name }}</h1>
        </div>
    </body>
    <script src="js/vue.js"></script>
    <script>
        
        var vm = new Vue({
            el: &#39;#app&#39;,
            data: {
                yes: true,
                no: false,
                age: 28,
                name: &#39;keepfool&#39;
            }
        })
    </script>
</html>

The display result is as follows,

Note: v-if instruction is executed based on the value of the conditional expressionInsertion or deletion behavior of elements.

1.2 v-for directive

v-for directive renders a list based on an array, which is similar to JavaScript’s traversal syntax :

v-for="item in items"

items is an array, item is the array element currently being traversed.

Sample code:


    
name age
{{item.name}} {{item.age}}

1.3 The v-bind directive can take a parameter after its name, separated by a colon. This parameter is usually an attribute of the HTML element, for example :v-bind:class

v-bind:argument="expression"

1.4 v-model

v-model (value, checked, and selected will be ignored after the form element is set), commonly used in Formsd5fd7aea971a85678ba271703566ebfd and 4750256ae76b6b9d804861d8f69e79d3

allow form elements and data to achieve two-way binding (mapping relationship)

Sample code

<p id="app">
    <p v-text="message"> </p>
    <input type="text" v-model="message">
</p>

</body>

<script type="text/javascript">    var app = new Vue({
        el:"#app",
        data:{
            message:"nice to meet you"
        }
    })</script>

1.5 The v-on directive is used to monitor DOM events. Its syntax is similar to v-bind. For example, to monitor the click event of the 3499910bf9dac5ae3c52d5ede7383485 element:

<a v-on:click="doSomething">

There are two forms of calling methods:Bind a method (let the event point to a reference to the method), or use an inline statement.
The Greet button binds its click event directly to the greet() method, while the Hi button calls the say() method.

It is a very common requirement to call event.preventDefault() or event.stopPropagation() in an event handler. Vue.js provides event modifiers for v-on. As mentioned before, modifiers are represented by instruction suffixes starting with a dot.

  • .stop

  • .prevent

  • .capture

  • .self

  • .once

  • .passive

<!-- 阻止单击事件继续传播 -->
<a v-on:click.stop="doThis"></a>

<!-- 提交事件不再重载页面 -->
<form v-on:submit.prevent="onSubmit"></form>

<!-- 修饰符可以串联 -->
<a v-on:click.stop.prevent="doThat"></a>

<!-- 只有修饰符 -->
<form v-on:submit.prevent></form>

<!-- 添加事件监听器时使用事件捕获模式 -->
<!-- 即元素自身触发的事件先在此处处理,然后才交由内部元素进行处理 -->
<p v-on:click.capture="doThis">...</p>

<!-- 只当在 event.target 是当前元素自身时触发处理函数 -->
<!-- 即事件不是从内部元素触发的 -->
<p v-on:click.self="doThat">...</p>

Vue.js is the two most commonly used instructions v-bind and v-on provides an abbreviation. The v-bind instruction can be abbreviated to a colon, and the v-on instruction can be abbreviated to the @ symbol.

<!--完整语法-->
<a href="javascripit:void(0)" v-bind:class="activeNumber === n + 1 ? &#39;active&#39; : &#39;&#39;">{{ n + 1 }}</a>
<!--缩写语法-->
<a href="javascripit:void(0)" :class="activeNumber=== n + 1 ? &#39;active&#39; : &#39;&#39;">{{ n + 1 }}</a>

<!--完整语法-->
<button v-on:click="greet">Greet</button>
<!--缩写语法-->
<button @click="greet">Greet</button>

1.6 The v-text directive is mainly to prevent {{}} from appearing on the page when the page is first loaded

v-text="expresstion"

Related recommendations:

ECMAScript Usage examples of typeof in

How does vue.js implement tree table encapsulation? How to implement tree table in vue.js

The above is the detailed content of Summary of commonly used instructions in Vue. 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