Toggling a modal for a child component in Vue.js

Parent component:

<template>
    <v-btn v-on:click="dialog = !dialog">
    </v-btn>
</template
<script>
    export default {
        data: () => ({
           dialog: false
        }
</script

Child component:

<template>
    <v-layout>
        <v-dialog v-model="toggledialog">
            <v-btn color="green darken-1" flat="flat" 
            @click.native="toggledialog = false">Close</v-btn>
        </v-dialog>
    </v-layout>
</template>
<script>
    export default {
        data: () => ({
            toggledialog: false,
        }),
        watch: {
            dialog: function(){
                this.toggledialog = true
            },
        },
        props:['dialog']
}
</script>

This code works but I am not a fan of using the watch workaround.

Questions:

  1. Is there a way to open the dialog in the child component without using props['dialog'] for
    v-model="toggledialog"</li>
    <li>How can I update the parent component's <code>dialog
    to false when it is changed in the child component with v-model="dialog", considering that Vue JS does not allow direct modification of props?

Answer №1

To make a dialog box close, pass the value prop as the value to the v-dialog component and re-emit the input event of the v-dialog when needed:

//CustomDialog.vue
<v-dialog :value="value" @input="$emit('input')">
    <v-btn color="green darken-1" flat="flat" 
        @click.native="$emit('input')"
    >Close</v-btn>
</v-dialog>
...
props:['value']

Then add the v-model directive to your parent component:

//Parent.vue
<custom-dialog v-model="dialog">

This way, you won't need any additional data or watchers.

Check out this example for reference

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

Establishing the value of "document.cookie"

Encountering issues while trying to set a cookie using different methods: Method 1: document.cookie = name + "=" + value + "; expires=" + date.toUTCString() + "; path=/"; This method only sets the value up to "name=value" wh ...

Using Middleware in Node JS Routes for Paginating Data Tutorial

Recently delving into Node and Express, I found myself integrating a pagination feature into my API. Although I've made significant progress, I'm facing challenges after refactoring and converting pagination into a function. This is the current ...

Passing parameters through the URL with Angular's ui-router

Instead of passing parameters when changing pages like this: $state.go('index.my_route', {my_param : value}); Is it possible to pass the parameter in the URL string while still using $state.go? For example: $state.go('index.my_route?my_pa ...

Vue ChartJS - Doughnut Chart with customized label in the center

I am interested in embedding text specifically within a Doughnut chart. Within my vuejs project, I am utilizing the following plugin: https://github.com/apertureless/vue-chartjs Presently, the text is displaying for all types of charts. My intention is ...

The ForEach function does not execute actions on every individual object, but rather only once

I am currently developing a Deck building application for a card game. In addition, I have set up a database to manage the cards that I sell and play with. The deck builder tool I created allows me to deplete the stock of sellable cards when adding them to ...

The URL is being modified by the Angular $compile function

I have a controller that is working well for dynamically compiling new DOM elements after certain ajax actions: angular.module('cms').controller('CompileHtmlCtrl', [ '$scope', '$compile', function ($scope, $comp ...

Send the express() app variable between files

Hey everyone, I understand there are many similar posts here, but unfortunately I'm still struggling with my issue. Within my server.js file, I have defined my app variable and implemented some configuration: var app = express(); ... In another fil ...

Using Javascript to Swap the Content of an Element

Recently, I've been delving into the world of javascript and have hit a roadblock. I understand how to instruct javascript to set a specific value for an ID, but now I have a new challenge: I want to create javascript code that can retrieve informati ...

What is the best way to merge angularjs modules?

In my angularjs application using the codeigniter PHP framework, I have implemented 3 modules - cars.js for car details, cellphones.js for cellphone details, and home.js for home details. Each module caters to a different client's needs. I am trying ...

Leveraging the power of moment to properly display time in relation to the present

Is there a way to format dates like "a few seconds ago", "10 minutes ago", "a day ago" using momentjs in the browser? var date = moment('2017-01-10T13:53:00'); date = moment(date).fromNow(); When I use date, it shows "in 14 minutes" instead of ...

Close the Hopscotch modal when moving to the next step or reaching the end

I'm having trouble figuring out how to close the modal after opening it. Initially, I can open the modal successfully using the following code: onNext: function() { $('#modal').modal('toggle'); } However, when attempting to cl ...

Take action upon being added to a collection or being observed

Consider the following scenario: I have an array called "x" and an Observable created from this array (arrObs). There is a button on the page, and when a user clicks on it, a random number is added to the array. The goal is to display the newly added value ...

Double your audio experience with Webaudio by playing the sound twice

While working on WebAudio experimentation, I encountered an issue with loading a sound using the JavaScript code below. function playAudio(){ var audio = document.getElementById('music'); var audioContext = new webkitAudioContext(); ...

Updating the dropdown title to reflect the chosen dropdown value

I'm currently learning react and attempting to grasp the concept through tutorials. However, I am facing challenges in changing the title of a dropdown list based on the selected value. I am using react bootstrap for this task, but struggling to imple ...

Determining the successful completion of an ajax request using jQuery editable plugin

I recently started using Jeditable to enable inline editing on my website. $(".video_content_right .project_description").editable(BASE_URL+"/update/description", { indicator: "<img src='" + BASE_URL + "/resources/assets/front/images/indicator ...

issue with vue v-on:click, interactions are not functioning

I recently started working with vuejs and I'm attempting to create a javascript shopping cart. However, I have encountered an issue with my method. The output is supposed to be an HTML table with some v-on:click actions that aren't functioning as ...

Terminate a browser tab with the click of an HTML button

I'm facing an issue with my HTML button - I need it to close the tab upon clicking. Unfortunately, the common methods seem to no longer work on newer versions of Chrome and Firefox. I've tried using these two solutions but they did not work: & ...

javascript href function usage

There's an issue I'm facing when using a link click to update a database field and redirect to another page. Here's the code I have: <a href="#" onclick="<?php $sql="UPDATE MyDB.mytable SET Date = '".da ...

Removing empty table rows using JavaScript when the page loads

Within my HTML table, I have noticed some empty cells and rows that need to be removed. To give an example of the code: <table id="7"> <tr> <td>Name</td> <td>Type</td> <td>Parent</td> <td>Time</td&g ...

Emptying the trumbowyg editor within an Angular environment

I am currently experiencing issues with the trumbowyg editor in a project I'm working on. I am able to update the editor by changing its model but cannot seem to clear its content using $scope.editorModel = '';. For a more detailed explanati ...