VUE JS uses components for two-way binding

Source: Internet
Author: User

1.VUE Front-end Simple introduction

VUE JS is a concise two-way data binding framework, his performance is more than ANGULARJS, because the implementation of the mechanism and ANGULARJS, he added to the data at initialization get and set method, when the data set, the data properties to add monitoring, so that the data changes, Will trigger the watcher above him, and Angularjs is implemented using dirty data checks.

In addition, Vuejs is easier to get started than Angularjs, and the Chinese documentation is complete.

2. Component implementation

in the development of VUE, we will need to extend some components to use in the form, such as a user selector.

Components and directives can be used in Vuejs encapsulation.

In Vuejs there is v-model this feeling and angularjs similar, actually completely different, no angularjs complex, he did not like Angularjs Ng-model Viewtomodel and Modeltoview characteristics, And this v-model can only be used on controls such as the input checkbox Select, while ANGULARJS extends Ngmodel implementation of his Render method.

In addition, I implement two-way binding when I use the Vue directive, which I have researched about custom-specified notation, which may be a less familiar cause and not yet implemented.

I use components to implement:

Vue.component (' Inputtext ', {                props:{' Input                 ':{true            },pname:                 { true             }},            ' <div><input type= ' text "v-model.lazy=" Input[pname] "><button @click =" Init "> Initialize </button></div>",            methods: {              init:function  () {                   this. input[. pname]= "Test";}}            )

When the Vue implements the component, he uses a unidirectional data stream, where we use the object to implement two-way binding.

In the above code, there are two properties:

Input,pname where input is an instance of a data object, PName: Just a string.

How to use:

<TableBorder= "1"width= "The ">         <TR>             <TD>Name</TD>             <TD>                 <Inputtext: Input= "Person"pname= "Name"></Inputtext>             </TD>         </TR>         <TR>             <TD>Age</TD>             <TD>                 <inputV-model= "Person.age">             </TD>         </TR>              </Table>     <TableBorder= "1"width= "The ">         <TR>             <TDcolspan= "3">                 <ahref="#"@click= "AddRow (' Items ')"class= "Btn btn-primary">Add to</a>             </TD>         </TR>                  <TRv-for= "(item, index) in Person.items">             <TD>                 <Inputtext: Input= "Item"pname= "School"></Inputtext>             </TD>             <TD>                 <Inputtext: Input= "Item"pname= "Year"></Inputtext>             </TD>             <TD>                 <a@click= "Removerow (' Items ', index)" >Delete</a>             </TD>         </TR>                   </Table>

<inputtext:input= "Item" Pname= "School" ></inputtext>

<inputtext:input= "Person" pname= "name" ></inputtext>

The component uses code, where Item,person data is bound, and PName is bound to a field.

JS Implementation code:

var New Vue ({                   data:{              person:{name:"", age:0,                items:[]                }          }          ,          methods: {                function  (name) {                this. Person[name].push ({school: "", Year: ""})              },              Removerow: function (name,i) {                  this. Person[name].splice (i,1);}}                 )        App8. $mount (' #app8 ')

Here we implement the data addition and deletion of the child table.

Interface effect:

VUE JS uses components for two-way binding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.