  South Square test tube   substituting  pregnancy  well  Division N

  South Square test tube   substituting  pregnancy  well  Division Micro signal N █ █: 138-0226-9370█ █ █ surrogate packet success packet healthy surrogate ██

Vue.js is the moment the fire of a JavaScript MVVM library, which is based on data-driven ideas and component-based construction. Compared to Angular.js, Vue.js provide a more concise, easier to understand the API, so that we can quickly get started and use Vue.js.

If you have before you get used to manipulate the DOM with jQuery, please put aside manually manipulate the DOM mind when learning Vue.js, because Vue.js is data-driven, you do not need to manually DOM. It is through some special HTML syntax, the DOM and data bind. Once you have created a bind, DOM and data will be synchronized whenever the data changes, DOM will be updated accordingly.

Of course, when using Vue.js, you can also be used in conjunction with other libraries, such as jQuery.

Demo and source code of this article has been put GitHub, if you feel good In this part, please choose a praise, or add a star on GitHub!

v-for Demo v-bind Demo Page Demo GitHub Source

MVVM pattern

FIG summarizes only the MVVM pattern (Model-View-ViewModel), and also describes how the Model View and interact in the ViewModel Vue.js.

 

ViewModel is the core Vue.js, which is a Vue instance. Vue example is the role a certain HTML, this element on the HTML element may be the body element, it can be designated an element id.

When you create a ViewModel, two-way binding is how to achieve it?

First, we will figure above DOM Listeners and Data Bindings as two tools that are key to the realization of two-way binding.
From the View side, ViewModel in the DOM Listeners tool will help change the DOM elements on the monitoring page us if there is a change, data Model of the change;
see from the Model side, when we update the data in the Model, Data Bindings tool It will help us update the DOM elements on the page.

Hello World example

Learn a language, or learning a new skill, write Hello World example is our only way.
This code outputs "Hello World!" On the screen.

<! DOCTYPE HTML>
 < HTML>
     < head> < Meta charset = "UTF-8"> < title> </ title> </ head> < body> <-! This is our View -> < div the above mentioned id = "App"> Message {} {} </ div> </ body> < Script the src = "JS / vue.js"> </ Script> < Script> // this is our the Model var exampleData = {Message: 'the Hello World!'} // create an instance or Vue "The ViewModel " // View which is connected with the Model new new Vue ({EL: '#app', data: exampleData }) </script> </html>

Vue process is defined using the process procedure MVVM each component part.

  1. View definitions
  2. Defines the Model
  3. Vue or create an instance of "ViewModel", and is used to connect Model View

When you create Vue instance, you need a pass options object , the object can contain data options, mount elements, methods, mold lifecycle hooks and so on.

In this example, the options object of el attribute points View, el: '#app'showing the Vue instance be mounted to <div id="app">...</div>this element; Data attribute points Model, data: exampleDatashowing our Model is exampleData object.
Vue.js multiple data binding syntax, the most basic form of text interpolated, using a pair of braces syntax, runtime {{ message }}will be replaced with the message attribute data objects, it will output "Hello World!" On the page.

Vue.js has been updated to version 2.0, but because the code is not the official version, this version is 1.0.25.

Two-way binding example

MVVM pattern itself is to achieve a two-way binding can be used in Vue.js the v-modelinstructions to create a two-way data binding on the form elements.

<!--这是我们的View-->
<div id="app">
    <p>{{ message }}</p> <input type="text" v-model="message"/> </div> 

The message text box bound to, when you change the value of the text box, <p>{{ message }}</p> the content will be updated.

1

Conversely, if you change the value of the message, the value of the text box will be updated, we can try in Chrome console.

2

Examples of attribute data Vue exampleData point, which is a reference type, change the properties of exampleData object data property also affect Vue instance.

Vue.js of common commands

Used above v-modelis a common instruction Vue.js, then what is it instruction?

When Vue.js instruction begins with v-, and their role offers some special features to HTML elements, instructions, binding instructions on the element, the instruction will add some special behavior for the target element binding, we the instructions may be considered as a special HTML attributes (attribute).

Vue.js provides some common built-in command, then we will introduce the following built-in instructions:

  • v-if instruction
  • v-show instructions
  • v-else instruction
  • v-for instruction
  • v-bind command
  • v-on instruction

Vue.js has good scalability, we can also develop some self-defined instructions, the latter article will explain the custom command.

v-if instruction

v-if是条件渲染指令,它根据表达式的真假来删除和插入元素,它的基本语法如下:

v-if="expression"

expression是一个返回bool值的表达式,表达式可以是一个bool属性,也可以是一个返回bool的运算式。例如:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8"> <title></title> </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('jack') >= 0">Name: {{ name }}</h1> </div> </body> <script src="js/vue.js"></script> <script> var vm = new Vue({ el: '#app', data: { yes: true, no: false, age: 28, name: 'keepfool' } }) </script> </html>

注意:yes, no, age, name这4个变量都来源于Vue实例选项对象的data属性。

image

这段代码使用了4个表达式:

  • 数据的yes属性为true,所以"Yes!"会被输出;
  • 数据的no属性为false,所以"No!"不会被输出;
  • 运算式age >= 25返回true,所以"Age: 28"会被输出;
  • 运算式name.indexOf('jack') >= 0返回false,所以"Name: keepfool"不会被输出。

注意:v-if指令是根据条件表达式的值来执行元素的插入或者删除行为。

这一点可以从渲染的HTML源代码看出来,面上只渲染了3个<h1>元素,v-if值为false的<h1>元素没有渲染到HTML。

image

为了再次验证这一点,可以在Chrome控制台更改age属性,使得表达式age >= 25的值为false,可以看到<h1>Age: 28</h1>元素被删除了。

3

age是定义在选项对象的data属性中的,为什么Vue实例可以直接访问它呢?
这是因为每个Vue实例都会代理其选项对象里的data属性。

v-show指令

v-show也是条件渲染指令,和v-if指令不同的是,使用v-show指令的元素始终会被渲染到HTML,它只是简单地为元素设置CSS的style属性。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8"> <title></title> </head> <body> <div 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> </div> </body> <script src="js/vue.js"></script> <script> var vm = new Vue({ el: '#app', data: { yes: true, no: false, age: 28, name: 'keepfool' } }) </script> </html>

image

在Chrome控制台更改age属性,使得表达式age >= 25的值为false,可以看到<h1>Age: 24</h1>元素被设置了style="display:none"样式。

4

v-else指令

可以用v-else指令为v-ifv-show添加一个“else块”。v-else元素必须立即跟在v-ifv-show元素的后面——否则它不能被识别。

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8"> <title></title> </head> <body> <div id="app"> <h1 v-if="age >= 25">Age: {{ age }}</h1> <h1 v-else>Name: {{ name }}</h1> <h1>---------------------分割线---------------------</h1> <h1 v-show="name.indexOf('keep') >= 0">Name: {{ name }}</h1> <h1 v-else>Sex: {{ sex }}</h1> </div> </body> <script src="js/vue.js"></script> <script> var vm = new Vue({ el: '#app', data: { age: 28, name: 'keepfool', sex: 'Male' } }) </script> </html>

v-else元素是否渲染在HTML中,取决于前面使用的是v-if还是v-show指令。
这段代码中v-if为true,后面的v-else不会渲染到HTML;v-show为tue,但是后面的v-else仍然渲染到HTML了。

image

v-for指令

v-for指令基于一个数组渲染一个列表,它和JavaScript的遍历语法相似:

v-for="item in items"

items是一个数组,item是当前被遍历的数组元素。

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="styles/demo.css" /> </head> <body> <div id="app"> <table> <thead> <tr> <th>Name</th> <th>Age</th> <th>Sex</th> </tr> </thead> <tbody> <tr v-for="person in people"> <td>{{ person.name }}</td> <td>{{ person.age }}</td> <td>{{ person.sex }}</td> </tr> </tbody> </table> </div> </body> <script src="js/vue.js"></script> <script> var vm = new Vue({ el: '#app', data: { people: [{ name: 'Jack', age: 30, sex: 'Male' }, { name: 'Bill', age: 26, sex: 'Male' }, { name: 'Tracy', age: 22, sex: 'Female' }, { name: 'Chris', age: 36, sex: 'Male' }] } }) </script> </html>

我们在选项对象的data属性中定义了一个people数组,然后在#app元素内使用v-for遍历people数组,输出每个person对象的姓名、年龄和性别。

image

View Demo

v-bind指令

v-bind指令可以在其名称后面带一个参数,中间放一个冒号隔开,这个参数通常是HTML元素的特性(attribute),例如:v-bind:class

v-bind:argument="expression"

下面这段代码构建了一个简单的分页条,v-bind指令作用于元素的class特性上。
这个指令包含一个表达式,表达式的含义是:高亮当前页。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="styles/demo.css" /> </head> <body> <div id="app"> <ul class="pagination"> <li v-for="n in pageCount"> <a href="javascripit:void(0)" v-bind:class="activeNumber === n + 1 ? 'active' : ''">{{ n + 1 }}</a> </li> </ul> </div> </body> <script src="js/vue.js"></script> <script> var vm = new Vue({ el: '#app', data: { activeNumber: 1, pageCount: 10 } }) </script> </html>

注意v-for="n in pageCount"这行代码,pageCount是一个整数,遍历时n从0开始,然后遍历到pageCount –1结束。

image

View Demo

v-on指令

v-on指令用于给监听DOM事件,它的用语法和v-bind是类似的,例如监听<a>元素的点击事件:

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

有两种形式调用方法:绑定一个方法(让事件指向方法的引用),或者使用内联语句。
Greet按钮将它的单击事件直接绑定到greet()方法,而Hi按钮则是调用say()方法。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8"> <title></title> </head> <body> <div 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> </div> </body> <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> </html>

5

v-bind和v-on的缩写

Vue.js为最常用的两个指令v-bindv-on提供了缩写方式。v-bind指令可以缩写为一个冒号,v-on指令可以缩写为@符号。

<!--完整语法-->
<a href="javascripit:void(0)" v-bind:class="activeNumber === n + 1 ? 'active' : ''">{{ n + 1 }}</a> <!--缩写语法--> <a href="javascripit:void(0)" :class="activeNumber=== n + 1 ? 'active' : ''">{{ n + 1 }}</a> <!--完整语法--> <button v-on:click="greet">Greet</button> <!--缩写语法--> <button @click="greet">Greet</button>

综合示例

现在我们已经介绍了一些Vue.js的基础知识了,结合以上知识我们可以来做个小Demo。

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="styles/demo.css" /> </head> <body> <div id="app"> <fieldset> <legend> Create New Person </legend> <div class="form-group"> <label>Name:</label> <input type="text" v-model="newPerson.name"/> </div> <div class="form-group"> <label>Age:</label> <input type="text" v-model="newPerson.age"/> </div> <div class="form-group"> <label>Sex:</label> <select v-model="newPerson.sex"> <option value="Male">Male</option> <option value="Female">Female</option> </select> </div> <div class="form-group"> <label></label> <button @click="createPerson">Create</button> </div> </fieldset> <table> <thead> <tr> <th>Name</th> <th>Age</th> <th>Sex</th> <th>Delete</th> </tr> </thead> <tbody> <tr v-for="person in people"> <td>{{ person.name }}</td> <td>{{ person.age }}</td> <td>{{ person.sex }}</td> <td :class="'text-center'"><button @click="deletePerson($index)">Delete</button></td> </tr> </tbody> </table> </div> </body> <script src="js/vue.js"></script> <script> var vm = new Vue({ el: '#app', data: { newPerson: { name: '', age: 0, sex: 'Male' }, people: [{ name: 'Jack', age: 30, sex: 'Male' }, { name: 'Bill', age: 26, sex: 'Male' }, { name: 'Tracy', age: 22, sex: 'Female' }, { name: 'Chris', age: 36, sex: 'Male' }] }, methods:{ createPerson: function(){ this.people.push(this.newPerson); // 添加完newPerson对象后,重置newPerson对象 this.newPerson = {name: '', age: 0, sex: 'Male'} }, deletePerson: function(index){ // delete an array element this.people.splice (index, . 1);}}}) </ Script> </ HTML>

6

In my view the Demo GitHub Pages:

View Demo

 
Category:  Vue

Guess you like

Origin www.cnblogs.com/DINGER01/p/10942540.html