Vuejs--v-on

Source: Internet
Author: User

Copyright Notice: Source http://blog.csdn.net/qq20004604

Directory (?) [+]

Information to the official documentation:

Http://cn.vuejs.org/guide/events.html

This article is based on the official documents, more detailed explanation, the code more complete.

Simply speaking, better for beginners to read

(22) method processorStandard usage of ①v-on

Used to listen to DOM events, typically V-on:click, the method of handling is placed in the methods attribute

A parameter is passed by default, with the following code:

    1. <button @click ="Test" > Click </button>
    2. Methods: {
    3. Test: function (evt) {
    4. Console.log (EVT);
    5. }
    6. }

Here the EVT is the standard mouse click event, similar to the parameters in the callback function of the Click event of jquery.

This can be used to find the value in the Data property (and perhaps several others), for example:

    1. Data: {
    2. Items: "Test"
    3. },
    4. Methods: {
    5. Test:function (evt) {
    6. Console.log (This.items);
    7. Console.log (EVT);
    8. }
    9. }

Here is the This.items, which is the data items of this variable;

② Inline Statement Processor

Pass a fixed parameter to the V-on event

    1. <button @click="Test (' a ')"> Click to search for age</button>

When this time, the first parameter of the function is not the mouse click event, but the string a

    1. Test: function (MES) {
    2. Console.log (MES);
    3. }

The value of MES is ' a '

$event

If you need to give him a mouse click event like the one above, use $event as the parameter (he is the same as the default mouse event object when the parameter is not passed);

Variables that use the Vue instance

If a value in the Data property needs to be passed, the property name is placed directly

For example:

  1. <div id="app" >
  2. <a href="http://www.baidu.com" @click ="Test (items, $event)" > click Search age</a>
  3. </div>
  4. <script>
  5. var test = {Name: "Test"};
  6. var vm = New Vue ({
  7. El: ' #app ',
  8. Data: {
  9. Items: "Test"
  10. },
  11. Methods: {
  12. Test: function (msg, evt) {
  13. Console.log (msg);
  14. Evt.preventdefault (); //block default actions, such as this is a page jump
  15. }
  16. }
  17. })
  18. </script>

Output: Test and button

③ event modifier (for v-on)

Modifier

Effect

Note

. prevent

Block default events for HTML elements

Similar to Evt.preventdefault ()

. Stop

Block event bubbling

KeyUp. Digital

When the key that the number represents is bouncing

Has an alias

Keyup.enter

Enter

When you press ENTER

Keyup.tab

tab button

When tab is in the input

Keyup.delete

Del key

Causes the original delete delete function to fail

Keyup.esc

ESC key

When the ESC is pressed

Keyup.space

Space key

Does not invalidate the space function (that is, when a space is pressed, both spaces and events are triggered)

Keyup.up

On the keyboard direction key

The top key also makes the cursor to the left of the input box

(The focus is in the input box, it takes effect when the key bounces, the same as below)

Keyup.down

The keyboard direction key is under

To the last face of the input box

Keyup.left

Left Arrow

Move Cursor Left

Keyup.right

Right Arrow

Move Cursor Right

. Self

Triggers an event when the current element itself (not a child element)

Looks like the top level when bubbling, typically used for mouse events such as click (after 1.0.16)

. Capture

Follow capture mode to handle

To put it simply, according to my assumptions, it is triggered by the capture sequence (the original pattern is after the capture bubbles first, this is the opposite) (1.0.16)


For. Self, for example, the following code:
  1. <div id="app" >
  2. <div @click. self="test" class="A" >
  3. <div class="B" >
  4. </div>
  5. </div>
  6. </div>
  7. <script>
  8. var test = {Name: "Test"};
  9. var vm = New Vue ({
  10. El: ' #app ',
  11. Data: {
  12. Items: "Test"
  13. },
  14. Methods: {
  15. Test: function (evt) {
  16. Console.log (EVT);
  17. }
  18. }
  19. })
  20. </script>

Events are triggered only when clicked on an area that is not div class= ' B ';

④ Custom Key aliases:

Specification:

Vue.directive ("on"), keycode. Alias = key code;

Example:

    1. Vue.directive ("on"). Keycode.z = 122;

This refers to a 122 (lowercase z) alias named Z, which triggers the event when the Z key of the keyboard is pressed (regardless of case).

Note that this is written after the instance declaration (presumably, the template containing the key is created before it is valid)

Vuejs--v-on

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.