What could be the reason for the child event not updating the state in the parent component (Vue)?

I have a component called customize-charts which contains a Vuetify drawer:

<template>
  <v-col>
    <v-btn style="float: right" class="mr-4 mt-2" small @click="toggleCustomize"  v-if="!open">Customize Dashboard</v-btn>
    <v-navigation-drawer
      v-model="open"
      temporary
      absolute
      right
      style="width: 25vw"
    >
      <span>draw contents</span>
      <v-divider />
    </v-navigation-drawer>
  </v-col>
</template>
<script>
export default {
  props: {
    data: {
      type: Array,
      default () { return [] }
    },
    open: {
      type: Boolean,
      default () { return false }
    }
  },
  data () {
    return {
      draggingItem: null
    }
  },
  methods: {
    toggleCustomize () {
      this.$emit('open')
    }
  }
}
</script>

It can be observed that the boolean "open" is the model that the drawer listens to and it is received from the parent component:

  <customize-charts v-if="chartCards.length" :data="chartCards" :open="customizePanel" @updateorder="updateOrder" @toggleshow="toggleShow" @open="customizePanel=!customizePanel"/> 

The parent component also includes the following:

{
  data () {
    return {
       customizePanel: false,
    }
  }
} 

However, there is an issue when the custom event open is triggered (@open="customizePanel=!customizePanel"); the drawer opens correctly, but it does not reset customizePanel to false when closed (when user clicks outside the drawer). Can you advise on how to address this?

Answer №1

The issue arises from your utilization of the open prop in conjunction with v-model. Props serve as a means of one-way data binding, intended for passing data solely from a parent component to a child component. In the child component, modifying the value of the prop is discouraged. If you inspect the browser Dev Tools, Vue likely generates a warning highlighting this behavior. Any changes made to the prop's value in the child component will be overwritten by the parent component's value upon the next rendering cycle...

Instead, consider implementing a computed property for v-model:

computed: {
  isOpen: {
   get() { return this.open }, // retrieve prop value
   set() { this.$emit('open') } // trigger an event and modify the prop value within the parent's event handler, allowing the updated value to propagate back to the child component
  }
}

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

Is there a way to activate the autoplay feature for this?

I'm really new to Jquery and most of this code isn't mine, I'm just using it as a learning tool for creating sliders. If someone could give me some guidance on how to make this slider work automatically when the page loads, that would be gre ...

Struggling to implement a vertical scroll bar in HTML code?

<body ng-app="myApp" ng-controller="myCtrl"> <div ng-show = "dataFromRest" ng-repeat = "x in dataFromRest.posts" > <div class="tittle" style="width: 25%;"> <a href="" ng-click="showDi ...

Having trouble adding elements (after the initial iteration) using the push() method in JavaScript

I'm facing an issue where I have a string and an array, but I'm unable to push elements after the first iteration. Below is the code I'm using: response1 = "hello"; var arr = [""]; arr.push(response1); console.log("First position " + arr[0 ...

Repairing the Left-sided Popup Leaflet

Can someone help me with fixing the positioning of the Popup to the left of the map? Currently, it does not stay aligned to the left edge when moving the map. I tried using position: fixed in my styling, and while everything looks good when zooming, it br ...

Implementing a dynamic autosuggest feature using jQuery and AJAX

I'm currently working on implementing an auto-suggest feature for dynamically added input boxes. While everything functions smoothly with static input types, I encounter issues when attempting to achieve the same functionality with dynamic inputs. Bel ...

Tips for streamlining a conditional statement with three parameters

Looking to streamline this function with binary inputs: export const handleStepCompletion = (userSave: number, concur: number, signature: number) => { if (userSave === 0 && concur === 0 && signature === 0) { return {complet ...

Error: Uncaught promise rejection - The function is undefined in the context of Vue.js and Electron

I've been experimenting with anime.js to animate elements using promise functions. However, I'm encountering an issue where the second function does not run after the previous one successfully completes. <script> import Splash from '. ...

Deploying an Express.js application: The command 'PassengerAppRoot' is invalid, possibly due to a misspelling or being defined by a module not included in the server configuration

I am looking to host an express.js app on cPanel. After successfully installing node, nvm, and npm, I managed to upload all the necessary files to the server and configure the .htaccess file. However, despite my efforts, cPanel's error logs are still ...

Determine if an Android application has been installed using JavaScript or jQuery

I'm working on an Android app that is also accessible via a web browser. I want to add a banner prompting users to install the Android application if they haven't already. How can I use JavaScript or jQuery to detect if the user has the app insta ...

Expiration Date of Third-Party Cookies

I need help retrieving the expiration date of a third-party cookie programmatically using JavaScript. Even though I can see the expiry time in the browser's DevTools (refer to the screenshot at https://i.sstatic.net/BW072.png), I am struggling to figu ...

Is there a way in Python to translate JavascriptSerializer into a datetime object?

My JSON file contains date and time in the format generated by JavascriptSerializer, shown below: {"StartDate": "/Date(1519171200000)/", "EndDate": "/Date(1519257600000)/",} How can I convert it to datetime formats like these? "2012-04-23T18:25:43.511Z" ...

Producing asynchronous JavaScript events using a browser extension (NPAPI)

Currently in the process of developing a web browser plugin using NPAPI. The issue I am facing is that my plugin requires a worker thread to handle certain tasks, and I need to pass events back to JavaScript as the worker progresses. However, due to the N ...

What is the best way to update $state in AngularJs when the user makes changes to the controller?

I am currently working on Angular UI Router and I want to refresh the current state by reloading it and rerunning all controllers for that state. Is there a way to reload the state with new data using $state.reload() and $stateParams? Here is an example ...

PHP and Vue component not displaying properly on webpage

I am having some trouble with my PHP code, as I am attempting to incorporate a Vue component into an HTML file. However, all I see is a blank page and there are no JavaScript errors appearing. Could someone please review the code below and let me know if ...

What could be preventing my component from importing properly in App.vue?

I am attempting to display a pulse loader while the page is loading. However, I encountered two errors in the code below. App.vue <template> <div id="app"> <div id="nav"> <router-link to='/'><div v-if="$ro ...

What is the best way to conceal a button before printing and have it reappear once the printing process is finished?

I have a button on my webpage with the id "print_req" that is used to trigger a JavaScript function for printing the page. I want this button to be hidden during the printing process and then reappear afterwards, so it does not show up in the printed docum ...

Is there a way to make sure that ngbpopovers stay open even when hovering over the popover content?

I have implemented a ngbpopover to display user information on an element. Currently, the popover is triggered on both hover and click events, but I would like it to remain open when hovered over, instead of closing as soon as the mouse moves away. How c ...

"Is there a way to decrease the width of the shadow cast behind the values in my Bar

Currently, I am utilizing BarChart (echarts) on my website. Is there a way for me to decrease the width of the shadow that appears behind the BarChart value? ...

Is there a way to dynamically apply multiple classes to a <span> element in Vue.js and Tailwind based on an element from an array?

I've got an array in my JavaScript file that contains a list of national parks along with their visitation status. var parksList = new Vue({ el: "#parks", data: { parks: [ { name: "Acadia", state: "Maine", s ...

showcasing asynchronous data attributes in Vue 3

After analyzing the provided sample code, I noticed that when using <p>{{usr[0]}}</p> it works as expected. However, when trying to access the 'title' property with <p>{{usr[0].title}}</p>, an error is thrown: 'Uncaug ...