1

I want to call a function on data change through v-model

HTML Part:

<input
  type="date"
  name="date"
  id="date"
  v-model="inputDate"
  @change="recallMeetingDetails"
/>

VueJS Part:

data(){
  return(){
    inputDate: new Date().toISOString().slice(0, 10),
  }
}
methods: {
  recallMeetingDetails(){
    console.log(this.inputData);
  }
}

Now this code works fine, but in the console, I am getting the following error:

[Vue warn]: You may have an infinite update loop in a component render function.

How can I do the functionality through any other method?

4 Answers 4

2

You can try like following snippet :

new Vue({
  el: '#demo',
  data(){
    return {
      inputDate: new Date().toISOString().slice(0, 10)
    }
  },
  methods: {
    recallMeetingDetails(date){
      this.inputDate = new Date(date).toISOString().slice(0, 10)
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <input
    type="date"
    name="date"
    id="date"
    :value='inputDate'
    @input="recallMeetingDetails($event.target.value)"
  />
  <h3>{{ inputDate }}</h3>
  
</div>

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

Comments

2

Using v-model is a great idea!

Use a watcher to watch the reactive data instead of @change on the input element, and call a function when the reactive variable changes: like this

<template>
<input
  type="date"
  name="date"
  id="date"
  v-model="inputDate"
  />
</template>

<script>
export default {
  data() {
    return {
      inputDate: new Date().toISOString().slice(0, 10)
    }
  },
  watch: {
    inputDate(value) {
      console.log(value)
    }
  }

}
</script>

Comments

0

v-model watches for the value and updates it in data, try to use v-bind:value="inputDate" instead of v-model

5 Comments

The problem with v-bind:value was that on changing the input value, in <input type=data/>, it doesnt change the data value inputDate as well. I need to change that value as well, coz i am calling a function with this.inputDate which in turn calls an API
of course it wouldn't, but you can change it by yourself recallMeetingDetails(data){ this.inputData = data console.log(this.inputData); }
So on doing the changes as follows: v-bind:value="inputDate" @change="recallMeetingDetails($event.target.value)" and then in the recallMeetingDetails(data), I am saving this.inputData = data; However, Vue JS still gives me the warning, Is it case of Vue JS issue or theres a specific way do the changes
Wait is it a mispelling with inputData? i can't see it in your code
Yeah sorry its inputDate. I managed to get the solution. Will be answering it here in sometime. Thanks for the help
0

So I managed to find a solution, the issue was in a different function.

In data(), I had 2 variables, which I was altering in a different function.

data(){
  return{
    inputDate: new Date().toISOString().slice(0, 10),
    topValue: 0,
    heightValue: 78,
  }
}

fnWithIssue(x,y){
  this.topValue = x + this.topValue;
  this.heightValue = y + this.heightValue;

 return{
   top: `${topValue}px`,
   height: `${heightValue}px`,
 }
}

Then in a template, I was passing the aforementioned return as Inline styling, the template was in turn inside a v-for, which caused the infinite loop

Instead I was able to fix the issue by removing the data's topValue and heightValue and just decalred them in the fnWithIssue(x,y)

fnWithIssue(x,y){
  let topValue = x + topValue;
  let heightValue = y + heightValue;

  return{
    top: `${topValue}px`,
    height: `${heightValue}px`
  }
}

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.