What's preventing the `@change` trigger from functioning properly with v-data-picker?

In my Vue.js application, I am utilizing the v-calendar package.

I am trying to pass selected date range values to the parent component. However, why is the @change trigger not working?

Parent.vue:

<template>
    <div>
        <Child @setRange="setRange" :range="range"/>
    </div>
</template>

<script>
data() {
    return {
        range: this.range,
    }
},
mounted() {
    firstCallToPage();
},
methods: {
    firstCallToPage(){
        axios.get('URL').then(response => {
            let self = this;
            this.range = {
                start: response.startDate,
                end: response.endDate,
            };
        }
    },
    setRange(range_value) {
        this.range = range_value;
    }
}
</script>

Child.vue:

<v-date-picker class='v-date-picker'
                       mode='range'
                       v-model='rangeValue'
                       :show-day-popover=false
                       :max-date='new Date()'
                       show-caps
                       :input-props='{placeholder: "", readonly: true}'
                       @change="sendRange">
</v-date-picker>
props: {
    range: {
        type: Object,
    },
},

data() {
  return {
    rangeValue: this.range
  }
},

sendRange: function () {
    this.$emit('setRange', this.rangeValue);
}

Error in console:

Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders.
Instead, use a data or computed property based on the prop's value. Prop being mutated: "range"

Answer №1

Consider using @input instead of @change. The use of @input is necessary for proper functionality in the v-datetime-picker component.

Answer №2

The error message clearly indicates the issue at hand. It seems that a prop is being passed to the child component (which includes v-date-picker) but then that prop is being overwritten by the use of v-model (v-model essentially serves as shorthand for :value and @change).

To address this, you should set your prop's value using a separate data property and utilize it in your operations:

<v-date-picker class='v-date-picker'
                       mode='range'
                       v-model='rangeValue'
                       :show-day-popover=false
                       :max-date='new Date()'
                       show-caps
                       :input-props='{placeholder: "", readonly: true}'
                       >
</v-date-picker>
props: {
    range: {
        type: Object,
    },
},

data() {
  return {
    rangeValue: this.range
  }
},

sendRange: function () {
    this.$emit('setRange', this.rangeValue);
}

Answer №3

Opt for an alternative approach by utilizing watch functionality instead of a traditional method...

Imagine you are dealing with the following attributes within the range Object

range: {
   start:value,
   end: value
}

<v-date-picker class='v-date-picker'
                       mode='range'
                       v-model='rangeValue'
                       :show-day-popover=false
                       :max-date='new Date()'
                       show-caps
                       :input-props='{placeholder: "", readonly: true}'
                       >
</v-date-picker>
props: {
    range: {
        type: Object,
    },
},
watch: {
  'rangeValue.start': function(newVal){
      this.$emit('setRange', newVal);
  }
},
data() {
  return {
    rangeValue: this.range
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using REST api with Vue.js for authentication process

Currently, I am working on a project utilizing vue cli and adonis.js. The frontend will be exclusively developed with Vue.js, while the backend will solely offer REST API services. My plan is to incorporate jwt for authentication purposes. However, I am fa ...

Tips for rearranging array position retrieved from API in Vue.js

props: { groups: Array, }, computed: { groups: function(arr) { alert('hi') } }, <div v-for="(item, index) in groups" :key="item.id" :id="item.id" class="group-name" @click="isOnlySelected ? null : $emit('setSele ...

Encountering a Parsing error while utilizing redux: Unexpected token present

Trying to implement Redux for managing the searchField state on the website. After creating action.js, reducer.js, constant.js, and redux.connect() function, An error occurred: Parsing error: Unexpected token 62 | return <div className=&apo ...

How to Dynamically Retrieve Keys from JSON Array in Javascript

Could you lend me your expertise by answering a query of mine? Here is a JSON array that I currently have: [{"A":20,"B":32,"C":27,"D":30,"E":40}] My goal is to pull out the keys (A, B, C, D, E) from this JSON array rather than the values. While I have m ...

Include a lower border on the webview that's being shown

Currently, the webview I'm working with has horizontal scrolling similar to a book layout for displaying HTML content. To achieve this effect, I am utilizing the scroll function. My inquiry revolves around implementing a bottom border on each page usi ...

The ng-click event in AngularJS does not function as expected when nested within another ng-repeat loop

I am facing an issue with ng-click in my Angular application (version 1.0.4). The first ng-click works fine, but the second one does not. <div class="menu-group" ng-repeat="module in modules"> <div ng-click="toggle($event, $parent)" ...

What is the best way to simultaneously create a customer and add a card with Stripe?

When attempting to initialize a customer for the first time, I encounter an issue where using a Stripe token more than once raises an error. The process involves a form submission on the client side and handling the token creation on the server side: var ...

I am having trouble getting the auto refresh feature in the div to work properly

As a newcomer to PHP and Javascript, I am trying to create a div that contains an ads script and refreshes every minute. I believe I have everything set up correctly in my code: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/ lib ...

Dealing with callback errors: error handling is anticipated

In my Vue web application, I have enabled ESLint and encountered an issue with the following code: myApi.get('products/12').then((prodResponse) => { state.commit('ADD_PRODUCT', {product: prodResponse.data}) }, error => { cons ...

Typescript code encountering unexpected behavior with Array.includes()

Below is a snippet of TypeScript code from my NextJS application: const holeSet:any[] = []; ..... let xx = 1, yy = 2; holeSet.push({x:xx,y:yy}); xx = 3; yy = 4; holeSet.push({x:xx,y:yy}); holeSet.map((e) => { console.log("element ::"+JSON ...

Enhancing the Strength of Password Generator

Example of a Simple Password Generator: function createPassword() { var characters = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOP" + "1234567890" + "@\#\-!$%^&*()_+|~=`{}\[\]:\";& ...

Tips for creating a TypeScript-compatible redux state tree with static typing and immutability:

One remarkable feature of TypeScript + Redux is the ability to define a statically typed immutable state tree in the following manner: interface StateTree { readonly subState1: SubState1; readonly subState2: SubState2; ...

The Material UI slider vanishes the moment I drag it towards the initial element

After moving the Material UI Slider to the initial position, it suddenly vanishes. via GIPHY I've spent 5 hours attempting to locate the source of the issue but have not had any success. ...

retrieve data from an external HTML file using JavaScript

Can someone assist me? I am working on a webpage that generates a table displaying high tide values in Venice from this link: http://93.62.201.235/maree/ESPORTAZIONI/MESE/Stazione_PuntaSalute_CanalGrande.html. I am trying to extract a specific value (the t ...

Guide to filtering out cookies using express-winston

After reviewing the README for express-winston, it appears that removing the headers from the logged line is quite straightforward: we can easily do so by adjusting the requestWhitelist option. However, this will disable logging all headers. Is there a wa ...

Developing dynamic forms within arrays using AngularJS

Hey there! I've got a scenario where I have an array of people in my dynamic application. Each person, such as James, Bob, and Walter, has their own set of data that needs to be filled out using simple directives. $scope.users = [ { name: ...

Executing multiple promise.all functions simultaneously

I have a large volume of 400 requests to a server, each encapsulated within a promise. However, when I attempt to run all 400 requests simultaneously using Promise.all, the system crashes. To address this issue, I decided to split the requests into batche ...

Dealing with Laravel and AJAX - Issue with Loading DIV

I find myself in a perplexing situation. Despite not encountering any errors in my apache logs or browser (chrome), I am faced with an issue that has left me baffled. On a specific page (localhost/admin/networks), I can click on an item from a database-ge ...

Passing events from Swift or Objective-C to JavaScript is a seamless process

A custom class was created with the following condensed version provided. For a reference to the full file, please visit this link. @objc(NativeMethods) class NativeMethods: RCTEventEmitter { @objc(sendEventToJSFromJS) func sendEventToJSFromJS { s ...

What are the best ways to engage with the Mixcloud Widget?

I have a component with a mixcloud embed code, which looks like this: <template> <div> <iframe id="widget" width="100%" height="60" :src="iframe.src" frameborder="0" v-show="iframe.loaded"></iframe> </div> </templ ...