[Vuejs]-How to preventDefault() in Vuejs?

2👍

Try to get the click event from the method :

methods: {
  startTimer: function (event) {
      var self = this;
      if (self.hour !== 0 && self.min !== 0 && self.sec !== 0) {
        event.preventDefault();
      } else {
 ...

and you shouldn’t use the () in the template in order to get the event passed as parameter :

<div @click="startTimer">add timer</div

or pass $event as parameter :

<div @click="startTimer($event)">add timer</div

1👍

call the function without the parenthesis ()

<div @click="startTimer">add timer</div

And then in the startTime function the event will automatically be injected by vuejs which you can use to preventDefault.

methods: {
  startTimer: function (event) {
      var self = this;
      if (self.hour !== 0 && self.min !== 0 && self.sec !== 0) {
        event.preventDefault();
      } else {
        this.interval = setInterval(function () {
          self.totalSeconds += 1;

          self.hour = Math.floor(self.totalSeconds / 3600);
          self.min = Math.floor((self.totalSeconds / 60) % 60);
          self.sec = parseInt(self.totalSeconds % 60);
        }, 1000);
      }
    },
}

More information

Leave a comment