3

With the following method "updateCoords", it is necessary to provide the 'event' argument. The following div tracks the mousemove event and updates the cursors x and y coordinates in real time via methods.

<div id="content">
  <div @mousemove="updateCoords($event)" style="height:400px;width:400px;border:2px solid black;">
    {{x}} {{y}}
  </div>
</div>

<script>
  new Vue({
    el: '#content',
    data: {
      x: 0,
      y: 0
    },
    methods: {
      updateCoords: function(event){
      this.x = event.clientX;
      this.y = event.clientY;
     }
    }
  })
</script>

This function does not work if event is not passed. It makes sense to me that we would pass something here since X and Y coordinates are dependent on the last location of the event. But how is this data stored? Do events track and store data within themselves?

Also it seems to make no difference if I pass my method the special $event variable or not. Calling...

  @mousemove="updateCoords($event)"
//is the same as 
  @mousemove="updateCoords()"

Does view automatically know that an incoming event argument is $event? Or is $event important to use in some other particular case??

1 Answer 1

6

By default, handlers receive the $event as their argument. You can use methods in inline handlers, in which case, if you want $event to be one of the arguments, you need to include it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.