6

I have a checkBox on vuejs. I call a function when it's status changes, but click event triggers before the v-model changes. So the value of v-model in the function is the previous value. How Can I make the click event trigger after the v-model's change?

var app = new Vue({
  el: '#app',
  data() {
    return {
      status: false
    }
  },
  methods: {
    checkBoxClicked() {
      console.log(this.status)
    }
  }
})
<link type="text/css" rel="stylesheet" href="https://unpkg.com/[email protected]/dist/css/bootstrap.min.css" />
<link type="text/css" rel="stylesheet" href="https://unpkg.com/[email protected]/dist/bootstrap-vue.css" />

<script src="https://unpkg.com/[email protected]/dist/vue.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/bootstrap-vue.min.js"></script>


<div id="app">
  <b-form-checkbox
      v-model="status"
      name="checkbox"
      @click.native="checkBoxClicked"
    >
      I accept the terms and use
   </b-form-checkbox>
</div>

2
  • move the logic of your click event to a watcher instead of handling the click event. Commented Oct 1, 2021 at 9:20
  • It is not such that easy in my project, needs so many changes. Commented Oct 1, 2021 at 9:27

2 Answers 2

6

Use change event instead of click:

<div id="app">
  <b-form-checkbox
      v-model="status"
      name="checkbox"
      @change="checkBoxClicked"
    >
      I accept the terms and use
   </b-form-checkbox>
</div>
Sign up to request clarification or add additional context in comments.

Comments

0

@atousadarabi How about if you wait until the next render cycle, does that help?

methods: {
   checkBoxClicked() {
      this.$nextTick(() => {
         console.log(this.status)
      })
   }
}

Otherwise just remove the click handler all together and have a watcher on status. Whenever it updates do what you need to :)

1 Comment

Unfortunately it didn't work for me.

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.