Summary of common instructions in vue.js
This time I will bring you a summary of the common instructions of vue.js. What are the precautions when using the common instructions of vue.js. The following is a practical case, let's take a look.
Vue.js is a very popular JavaScript MVVM (Model-View-ViewModel) library. It is built with data-driven and componentized ideas. Compared with Angular.js, Vue.js provides a simpler and easier-to-understand API, allowing us to quickly get started and use Vue.js.
If you have been accustomed to using jQuery to operate the DOM before, please put aside the idea of manipulating the DOM manually when learning Vue.js, because Vue.js is data-driven, and you do not need to manually operate the DOM. It binds DOM and data through some special HTML syntax. Once you create the binding, the DOM will stay in sync with the data, and whenever the data changes, the DOM will be updated accordingly.
Of course, when using Vue.js, you can also use it in conjunction with other libraries, such as jQuery.1.Use
The process of using Vue is the process of defining the various components of MVVM (Model-View-ViewModel).<!--这里定义View--> <p id="app">{{ message }}</p> <script src="js/vue.js"></script> <script> // 这里定义Model var exampleData = { message: 'Hello World!' } // 这里创建一个 Vue 实例或 "ViewModel" // 连接 View 与 Model new Vue({ el: '#app', data: exampleData }) </script>
2. Common instructions of Vue.js
Vue.js provides some commonly used built-in instructions. Next we will introduce the following built-in instructions: •v-if command•v-show command
•v-else command
•v-for command
•v-bind command
•v-on command
2.1 v-if directive
v-if is followed by an<p 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('jack') >= 0">Name: {{ name }}</h1> </p> <script src="js/vue.js"></script> <script> var vm = new Vue({ el: '#app', data: { yes: true, no: false, age: 28, name: 'keepfool' } }) </script>
<p id="app"> <h1>Hello, Vue.js!</h1> <h1>Yes!</h1> <!----> <h1>Age: 28</h1> <!----> </p>
2.2 v-show
<p id="app"> <h1>Hello, Vue.js!</h1> <h1 v-show="yes">Yes!</h1> <h1 v-show="no">No!</h1> <h1 v-show="age >= 25">Age: {{ age }}</h1> <h1 v-show="name.indexOf('jack') >= 0">Name: {{ name }}</h1> </p> </body> <script src="js/vue.js"></script> <script> var vm = new Vue({ el: '#app', data: { yes: true, no: false, age: 28, name: 'keepfool' } }) </script>
<p id="app"> <h1>Hello, Vue.js!</h1> <h1>Yes!</h1> <h1 style="display: none;">No!</h1> <h1>Age: 28</h1> <h1 style="display: none;">Name: keepfool</h1> </p>
2.3 v-else instruction
<h1 v-if="age >= 25">Age: {{ age }}</h1> <h1 v-else>Name: {{ name }}</h1> <script src="js/vue.js"></script> <script> var vm = new Vue({ el: '#app', data: { age: 28, name: 'keepfool', sex: 'Male' } }) </script>
2.4 v-for directive
<p v-for="p in people"> <h1>Age: {{ p.age }}</h1> <h1>Name: {{ p.name }}</h1> <h1>Sex: {{ p.sex }}</h1> </p> <script charset="utf-8" src="js/vue.js"></script> <script type="text/javascript"> var myModel = { people:[ { age: 25, name: 'keepfool', sex: 'Male' }, { age: 26, name: 'keepfool2', sex: 'FeMale' }, { age: 27, name: 'keepfool3', sex: 'Male2' } ] }; var vm = new Vue({ el: '#app', data: myModel }) </script>
<p v-for="(item, index) in items"></p> <p v-for="(val, key) in object"></p> <p v-for="(val, key, index) in object"></p>
2.5 v-bind command
The v-bind directive can take a parameter after its name, separated by a colon. This parameter is usually an attribute of the<p id="app"> <ul class="pagination"> <li v-for="n in pageCount"> <a href="javascripit:void(0)" rel="external nofollow" v-bind:class="activeNumber === n ? 'active' : ''">{{ n }}</a> </li> </ul> </p> <script src="js/vue.js"></script> <script> var vm = new Vue({ el: '#app', data: { activeNumber: 1, pageCount: 10 } }) </script>
<img v-bind:src="'/path/to/images/' + fileName"> <p v-bind="{ 'id': someProp, 'other-attr': otherProp }"></p>
2.6 v-on command
<p id="app"> <p><input type="text" v-model="message"></p> <p> <!--click事件直接绑定一个方法--> <button v-on:click="greet">Greet</button> </p> <p> <!--click事件使用内联语句--> <button v-on:click="say('Hi')">Hi</button> </p> </p> <script src="js/vue.js"></script> <script> var vm = new Vue({ el: '#app', data: { message: 'Hello, Vue.js!' }, // 在 `methods` 对象中定义方法 methods: { greet: function() { // // 方法内 `this` 指向 vm alert(this.message) }, say: function(msg) { alert(msg) } } }) </script>
2.7 v-model directive
v-model creates two-way data binding on form control elements<input v-model="message" placeholder="edit me"> <p>Message is: {{ message }}</p>
The above is the detailed content of Summary of common instructions in vue.js. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
