Basically is according to the official website Guide all combed again: http://vuejs.org/guide/index.html here we take a Todo List application as an example to all the related only string up, this article inside all the code is on the GitHub https:/ /github.com/lihongxun945/vue-todolist
Vue instance
A Vue application is initiated by a root Vue instance boot, and Vue instance is created this way:
var vm = new Vue ({
//Options
})
A instance is actually a VM in the MVVM. All the properties in data in the incoming configuration object are mounted on the instance, and in order to avoid naming conflicts, Vue built-in methods are mounted to the instance with an attribute that begins with $.
Instance from creation to destruction will experience the following lifecycle:
Approximately three steps are passed when initializing:
• Binding data monitoring, i.e.
• Compiling templates
• Insert document or replace the corresponding DOM
# Vue Basic Syntax
Data binding
Vue uses a class mastache syntax. Common binding syntax is divided into several categories:
Mastache syntax, such as {{data}} {Data | filter}}
V-bind binding attributes, such as V-bind:href, V-bind:class
v-on binding events, such as V-on:click, V-on:submit
Where v-* are all directive
Example:
<div v-bind:class= "[ClassA, IsB? CLASSB: ']" >
Property calculation
Vue supports an interesting property calculation syntax, which allows you to specify that a property is calculated by another property, so that you do not have to do it by $watch:
var vm = new Vue ({
el: ' #example ',
data: {
a:1
},
computed: {
//a computed getter
b:functio N () {
//' This ' points to the VM instance return
THIS.A + 1
}}}
)
# # Process Control and list-related syntax includes ' v-if ', ' v-show ', ' v-else ', ' v-for '
Form
Bidirectional data binding:
<input type= "text" v-model= "message" placeholder= "Edit Me" >
<input type= "checkbox" id= "Jack" value= "Jack" v-model= "Checkednames" >
# # Animation animation is implemented in the same way as angular and react, are implemented by adding and removing class. # Component
Basic usage of components
The definition of Component consists of two parts:
1 Create the Component class:
var profile = Vue.extend ({
Template: "<div> Lily </div>"
});
2 Register a tagname:
Vue.component ("Me-profile", profile);
So we can use this component through TagName:
<div id= "Todo" >
<my-profile></my-profile>
<form v-on:submit= "Add" v-on: submit.prevent>
<input type= "text" v-model= "input"/> <input type=
"Submit" value= ' Add '/>
</form> ...
</div>
Vue.component ("Me-profile", profile); is a global registration, if used only in a certain page, you can register by local way:
var vm = new Vue ({
el: "#todo",
components: {
"my-profile": Profile
},
...
}
Because our Vue instance is bound to the TODO element, it is not valid to place the my-profile outside of this element, only to be placed inside this to be initialized by this instance of Vue.
Precautions:
The parameters that can be passed by the Vue constructor can be used primarily on vue.extend, but for El and data two parameters, to avoid sharing the same object between different instances, it is always possible to return a new object through the function:
var mycomponent = vue.extend ({
data:function () {return
{a:1}}}
)
Because the parameters are the same, in fact they are the same thing, but one is a component, one is used to guide the Vue boot.
Templates considerations
Because Vue is the native DOM, some custom tags may not conform to the DOM standard, such as customizing a TR in a table, and if inserting the my-component does not conform to the specification, you should write this:
<table>
<tr is= "my-component" ></tr>
</table>
Props Pass Data
Each component in Vue is independent and cannot and should not directly access the data of the parent class. So we pass the props to the subassembly, does it look like react?
Unlike react, the Vue neutron component needs to declare its own props before it can:
var profile = Vue.extend ({
props: ["name"],
Template: '
Then we can pass the parameters like this when we use profile:
<my-profile name= ' Lily ' ></my-profile>
This is passed by literal parameters, so the value passed must be a string. There is also a way to dynamically pass parameters through V-bind to bind data or to pass a non string parameter in two directions:
<my-profile v-bind:name= ' input ' ></my-profile>
V-bind if it is a string, it is the corresponding field in the data of the binding parent component, such as the value of the input that is two-way bound. If it's a number, it's bound to a number.
Vue can also explicitly specify one-way or two-way data binding:
<!--default, One-way-down binding-->
<child:msg= "parentmsg" ></child>
<!--explicit Two-way binding-->
<child:msg.sync= "parentmsg" ></child>
<!--Explicit one-time binding-- >
<child:msg.once= "parentmsg" ></child>
Props Check
A good component should always verify that the parameters are correct, and you may need to set the default values for some parameters:
var profile = Vue.extend ({
input: {
type:string
}}
);
Parent-Child Component communication
The props mentioned above is actually a way for a parent component to deliver messages to subcomponents.
In a subassembly there is a this. $parent and this. $root can be used to method parent components and root instances. However, we should avoid doing so now. Because the component itself is designed to encapsulate independent logic, the encapsulation of the component is compromised if the data is accessed directly from the parent component.
So we should still communicate through the parent component to the child component by passing the props way.
Of course props can only do a callback. This problem has been discussed in react, react's approach is to do by props, passing a callback function to the subassembly. In fact, I do not like the way the callback function is transmitted, I prefer the way the event. Vue A neutron component can communicate by using events and parent components. Sending a message to a parent component is through this. $dispatch, which sends a message to a subassembly through this. $boardcast, where messages are sent to all fathers and children, but once a callback is executed, it stops unless the callback function explicitly returns TRUE.
We've split the previous Todo List into different components so that we can experience the two-way communication of the components, and we split up two components, the list and Form, respectively.
The form is responsible for processing user input and sends an add message to the parent component when submitting the form, as follows:
var Form = vue.extend ({
props: {
username: {
type:string,
default: ' Unnamed '
}
},
data: function () {return
{
input: "",
};
},
Template: '
List is only for displaying lists and handling user tick actions, and it receives the add message and adds an entry to itself:
var List = vue.extend ({
Template: '
<ul>
<li v-for= ' todo in List ' >
<label v-bind:class= ' {done:todo.done} ' >
<input type= ' checkbox ' v-model= ' Todo.done '/>
{{todo.title}}
</label >
</li>
</ul> ',
props: {
initlist: {
type:array
}
},
data: function () {return
{
list: []
}
},
events: {
add:function (input) {
if (!input) return false;
This.list.unshift ({
title:input,
done:false
});}}
);
Then, since this is two components, and of course it takes a Vue instance to boot, our example is as follows:
var vm = new Vue ({
el: "#todo",
components: {
"todo-form": Form,
"todo-list": List
},
Events: {
add:function (input) {this
. $broadcast ("Add", input);}}
);
Note that the Form and List are logically peer components, so they don't have a parent-child relationship, and they are all VM children together. Here the VM receives a message from Form and then it is forwarded to the List.
The HTML code is much simpler:
<div id= "Todo" >
<todo-form username= ' Lily ' ></todo-form>
<todo-list></ Todo-list>
</div>
Slot
It is not clear when it is necessary to insert the HTML rendered by the parent component into a subassembly by Slot, and it is too intrusive for the subassembly to do so.
Dynamic Switching components
This feature feels a bit redundant, and it feels like in many cases we should switch through logical code instead of VUE the built-in dynamic components. However, it is convenient to implement a tab-like switch function.
Here we add an about page to the Todo List. So first we need to change the VM to a component called Todo, which is the entire TODO page:
var Todo = vue.extend ({
Template: '
<div id= ' todo ' >
<todo-form ' Lily ' username= todo-form>
<todo-list></todo-list>
<slot>not show</slot>
</div>
',
components: {
"todo-form": Form,
"todo-list": List
},
events: {
add: function (input) {this
. $broadcast ("Add", input);}}
);
In fact, the first line of change.
Then we need to create an about component:
var about = Vue.extend ({
Template: '
<div id= ' about ' >
<p>about Todo List v0.1.0</p>
<p>content here</p>
</div> '
});
The next point is that we're going to create an instance VM, which is responsible for switching between the two pages:
var vm = new Vue ({
el: "Body",
data: {
currentview: "Todo"
},
components: {
"todo": todo,< c15/> "about": About
}
);
Here we define a currentview field, which, of course, can be any name, and then switch components through a special component tag:
<component:is= "CurrentView" ></component>
<ul>
<li><label><input type= " Radio "Name= ' page ' value= ' todo ' v-model= ' CurrentView ' > home</label></li> <li><label
><input type= "Radio" name= ' page ' value= ' about ' v-model= ' CurrentView ' > about</label></li>
</ul>
The above code has two places to note:
• By component This special label, and then using the: Is property for component switching.
Radio modifies the CurrentView field by bidirectional binding, enabling you to switch after clicking.
The implementation principle of data binding
Vue refers to bidirectional bindings as reactive and can be translated into responsive data binding. The interior is implemented through the ES5-defined getter and setter methods, so the IE8 and the following browsers are not supported, and this implementation has two error-prone areas:
• If you add and remove attributes directly on data that cannot be detected, general deletions are not, but may be added dynamically, and this time should be added by means of VM. $set ("name", value).
• Unable to detect changes within the object, that is, only the properties of the data can be detected, if DATA.A is an object, then data.a.b = 1 This change cannot be detected. In this case, you should create a new object and assign the value to DATA.A.
Asynchronous update mechanism
Vue updates to the DOM are asynchronous! This asynchronous is done in an asynchronous queue, but this asynchronous queue is executed in the current Event Loop, so it is not right to modify the Data immediately to query in the DOM, and the DOM is not updated at this time, and the correct way to do this is to:
vm.msg = ' new message '//change data
vm. $el. textcontent = = ' new message '//False
Vue.nexttick (function () {
vm. $el textcontent = = ' new message '/True
})
or so:
VM. $nextTick (function () {this
. $el. textcontent = = ' new message '/True
})
It took half a day to see the components, and the following should look at another key point: directive
This article has been organized into the "Vue.js front-end component Learning course", welcome to learn to read.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.