How can I interact with a v-dialog component within a child component in Vue.js using Vuetify?

Exploring Vue.js for the first time and hoping to display a login dialog upon button click. To maintain cleanliness in my code, I shifted the dialog to a child component within a parent component with nested LoginDialog. Below are snippets of the parent component code:

 <div class="my-2 mx-10">
    <v-btn color="#004a04" @click="showLoginDialog">
        <p class="my-2">SIGN IN</p>
    </v-btn>
  </div>
 .... 
  showLoginDialog() {
      this.loginDialogVisibility = true;
  },
  login(username, password) {
      this.loginDialogVisibility = false;
      //login functionality
  }

As for the child component:

<template>
<div>
    <v-dialog v-model="visibility" max-width="300px">
        <v-card class="d-flex flex-column" height="400px">
    <v-card-title>
      <span class="headline">Sign in</span>
    </v-card-title>

    <v-col class="d-flex justify-center">
      <v-card-text>
        <v-text-field v-model="username" label="Username"></v-text-field>
        <v-text-field v-model="password" :type="'password'" label="Password"></v-text-field>
      </v-card-text>
    </v-col>

    <v-col class="d-flex justify-center">
      <v-card-actions class="card-actions">
        <v-btn text color="primary" @click="login">SIGN IN</v-btn>
      </v-card-actions>
    </v-col>
  </v-card>
</v-dialog>
</div>

export default {
    name: "LoginDialog",
    data() {
        return {
            username: null,
            password: null
        }
    },
    props: {
        dialogVisibility: {
            type: Boolean,
            default: false
        }
    },
    methods: {
        login() {
            this.visibility = false;
            this.$emit("login", this.username, this.password);
        }
    },
    computed: {
        visibility() {
            return this.dialogVisibility;
        }
    }
}
</script> 

Encountering an issue where the loginDialogVisibility parent variable only changes to false when closing the dialog using the "sign in" button. If closed by clicking on the background, loginDialogVisibility remains true preventing me from re-rendering the modal by clicking the button again. How can I establish proper communication in such situations? What am I missing here?

Answer №1

Hey there! Make sure to include the "emit" method in your child component.
Just a friendly reminder, you shouldn't specify the position of the child component within the parent component. Are you looking to close the modal once the Login Button is triggered?

Here's a suggested approach.

// Parent Component 
<template>
<div class="my-2 mx-10">
    <v-btn color="#004a04" @click="showLoginDialog">
        <p class="my-2">SIGN IN</p>
    </v-btn>
    <child-component @show="showDialog" />
</div>
</template>

<script>
// method emitted by the child to the parent
showDialog(value) { 
      // value == true if Login is clicked
      this.loginDialogVisibility = value; 
  }
</script>


// Child Component
<template>
<v-card-actions class="card-actions">
    <v-btn text color="primary" @click="login">SIGN IN</v-btn>
</v-card-actions>
</template>

<script>
methods: {
    login(){
    ...your logic...
    // emit false value to parent to close the dialog
    this.$emit('show', false) 
    }
}
</script>

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

Buefy fails to respond to Sass styling efforts

Hey there, I'm new to Vue and I've set up a simple Vue (2) app with node-sass and sass-loader following the instructions here. I have Buefy imported in main.js and below is an excerpt from my App.vue file: <style lang="scss"> // ...

Creating a JSON array using looping technique

I am attempting to construct a JSON array using a loop where the input className and value will serve as the JSON obj and key. However, I am facing difficulties in creating one and cannot seem to locate any resources on how to do so. Below is an example sn ...

"Encountering a problem with using setState in React Hook useEffect

I am currently utilizing the useState hook with two arrays: imageList and videoList. In my useEffect hook, I iterate through the data using forEach method. If the type of the item is an image, it should be pushed to the imageList array. However, after exec ...

Caution: PHP's move_uploaded_file() function is unable to successfully relocate the audio file

I've implemented a straightforward Record Wave script using Recorder.js Encountering an Issue Recording works fine Playback of my recording is successful Downloading the recorded file from blob works smoothly The problem arises when trying to uploa ...

How can you convert a JavaScript object with nested arrays into JSON format?

I have been working with a JavaScript object that contains nested objects with associative arrays. I attempted to use the stringify function from the json2.js library, but the output did not include the arrays inside the nested objects. In my scenario, I b ...

Ways to trigger a server function in JavaScript upon closing a browser tab or the entire browser

How can I trigger a server function when closing a browser tab or window using JavaScript? Similar to this image //Below is the code for my server-side function: public ActionResult DeleteNotPostedImage(string folder , string PostID) { folder ...

Third Party Embedded Video requiring SSL Certificate

I am currently facing an issue where I am trying to embed videos from a website that does not have an SSL certificate. This is causing my own website to appear insecure when the page with the embedded video loads, despite the fact that my website has its ...

Utilizing AngularJS to dynamically inject HTML content into $scope

In my possession are the following files: index.html //includes instructions for passing arguments to the btnClick function in app.js <div ng-bind-html="currentDisplay"></div> app.js app.factory('oneFac', function ($http){ var htm ...

Having trouble with res.redirect not working after the page has been rendered with data?

I have a basic forget password feature set up, where users can request a password change and receive an email with a token. Clicking the link in the email will redirect them to a page where they can input their new password. When I click on the email link ...

Separating buttons (two buttons switching on and off simultaneously) and displaying the corresponding button based on the data

My current project involves creating a registration page for specific courses. While I am able to display the information correctly, I am facing an issue with the ng-repeat function. The problem lies in all the Register buttons toggling incorrectly. Additi ...

Ways to customize PreBid.js ad server targeting bidder settings

In an effort to implement a unique bidder setting key name within my prebid solution, I have taken the necessary steps as outlined in the documentation by including all 6 required keys. Is it possible to change the key name 'hb_pb' to 'zm_hb ...

Is there a way to live filter results using Vue?

Looking to apply a filter on my input v-model to search through a list of products. The current filter only shows items with an exact match to the input. How can I adjust it to display partial matches as the user types? Vue.filter('byName', fun ...

Horizontal rule located within a table but spanning the entire width

I wrote the following code: <table> {item.awards.map((obj,i) => <tbody> <tr> <td>Name</td> <td>:</td> <td>{obj.title}</td> </tr> ...

Contradictory jQuery extensions

After exploring this site as part of my web coding self-study, I decided to register so I could ask a specific question. Currently, I am in the process of developing my new website and ran into an issue on a test page (www.owenprescott.com/home.html). The ...

Ajax is known for inundating servers with requests at a rapid rate, causing a significant

I am running an application that sends multiple requests per second to fetch data from API and return responses to clients. The process flow is as follows: Ajax sends a request View sends a request API returns response for view View returns response for ...

What is the best way to extract ABC 12005 from strings like ABC000012005 and ABC0000012005?

My task involves parsing a string with values like ABC000012005,ABC0000012005. The desired output is to extract the prefix and numbers without leading zeros, formatted as ABC 12005, ABC 12005 ...

Steps to Create a Repeating Melody/Instructions for Implementing a Loop Function

Currently, I am working with discord.js and have had success implementing various commands. However, I am having trouble creating a loop command for my discord bot. This specific command should play a song repeatedly until the loop command is executed ag ...

Exploring the functionality of promises in JavaScript

Currently, I am using the most recent version of Angular. The code snippet I've written looks like this: $q.all({ a: $q.then(func1, failHandler), b: $q.then(func2, failHandler), c: $q.then(func3, failHandler), }).then(func4); Is it guaranteed ...

Improving nested v-models using computed properties in Vue.js 2 instead of watchers

Consider this simplified VueJS component example: MyForm (parent component) <template lang="pug"> user-component(v-model="apiData.user") </template> <script> export default { name: "MyForm", component ...

Exploring AngularJS: A Guide to Accessing Millisecond Time

Is there a way to add milliseconds in Time using AngularJS and its "Interval" option with 2 digits? Below is the code snippet, can someone guide me on how to achieve this? AngularJs Code var app = angular.module('myApp', []); app.controller(&ap ...