Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger a vue method only once, not every time

I'm handling a rotate even on change:

<div @change="handleRotate"></div>
<script>
export default {
  data: {
    rotate = 0
  },
  methods: {
    handleRotate () {
      this.rotate = this.rotate + this.getRotateAngle(e.clientX, e.clientY)
    }
  }
}
</script>

Right now, the second this.rotate runs on every change. How can I do it so that the second this.rotate is applied only the first time handleRotate runs?

like image 909
alex Avatar asked Dec 23 '16 07:12

alex


People also ask

How do you stop event propagation in Vue?

Specifically, we can use stop event modifier. . stop will stop the event propagation.

What is click prevent VueJS?

. prevent is a modifier for v-on:click . Another handy modifier is . self , which tells Vue to only evaluate v-on:click if the element itself is clicked, as opposed to one of its children. For example, Vue only calls the below v-on:click handler when you click on the outer div , not the inner div .

How can you prevent layout jumps in VUE JS?

Use single file components if you can. If you come from React you can also find jsx for Vue useful, or even write render function by hand (though not recommended). If you want to totally nullify the time of the first rendering though, the only way to go is to do server-side rendering.

How do you trigger a watch in Vue?

To trigger the Vue watch method, you should assign a new city name to the value property of the ref object. Do not reset the city variable itself.


2 Answers

Solving it Vue way:

You can use $once, which will listen for a event but only once.

Listen for a custom event, but only once. The listener will be removed once it triggers for the first time.

You just need to add .once to @change like following:

<div @change.once="handleRotate"></div>
<script>
export default {
  //No Change here
}
</script>

Check demo if this in the fiddle.


Old Answer:

If you do not want to have initial value set for rotate, you can have one more variable : hasRotated to track whether rotate has been changed or not. Initially set hasRotated to true, once rotate has been changed set hasRotated to false, like following:

<div @change="handleRotate"></div>
<script>
export default {
  data: {
    rotate: 123,
    hasRotated: false
  },
  methods: {
    handleRotate () {
      if(this.hasRotated){
        this.rotate = this.rotate + this.getRotateAngle(e.clientX, e.clientY)
        this.hasRotated = false
      }
    }
  }
}
</script>
like image 179
Saurabh Avatar answered Oct 13 '22 00:10

Saurabh


one simple solution would be to add a marker somewhat like this:

<script>
export default {
  data: {
    rotate = 0
  },
  methods: {
    handleRotate () {
      if(!this.rotated){
          this.rotate = this.rotate + this.getRotateAngle(e.clientX, e.clientY);
          this.rotated = true;
      }
    }
  }
}
</script>

of course you would need to initiate this.rotated as false

like image 43
Victor Radu Avatar answered Oct 12 '22 23:10

Victor Radu