Implementation of observer mode in vue

Source: Internet
Author: User

There are three ways to implement the observer mode in vue:

1. V-On Method

Exp:

<Div id = 'test'>

<Button V-on: event = 'functionname'> buttonname </button>

</Div>

VaR Vm = new vue ({

El: '# test ',

Method :{

Functionname: function (){};

}

}

V-on can bind events in the doc to the corresponding function. when an event occurs, the bound function will be called!

2. compute attributes

<Div id = 'test'>

<P >{{ fullname }}</P>

</Div>

VaR Vm = new vue ({

El: '# test ',

Data :{

Firstname =''

Lastname =''

}

Compute :{

Fullname: function (){

Return firstname + lastname;

}

}

}

Every time firstname and lastname are updated, fullname will be called. In fact, VM. fullname. getter () is called ().

3. Use the watch attribute

<Div id = 'test'>

<P >{{ fullname }}</P>

</Div>

VaR Vm = new vue ({

El: '# test ',

Data :{

Firstname = '',

Lastname = '',

Fullname =''

}

Watch :{

Firstname: function (){

Fullname = firstname + lastname;

Return;

},

Lastname: function (){

Fullname = firstname + lastname;

Return;

}

}

}

Whenever the observed value in watch changes, the corresponding function will be called.

Implementation of observer mode in vue

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.