"Patience is key when waiting for the alert dialog response in Vuetify

I currently have a reusable component called Alert.vue.

    <v-dialog v-if="alertDict" v-model="alertDict.showDialog" max-width="460">
      <v-card>
        <v-card-title>Title</v-card-title>
        <v-card-text>Message</v-card-text>
        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn olcor="green darken-1" text @click="alertDict.showDialog = false">Ok</v-btn>
          <v-btn olcor="green darken-1" text @click="alertDict.showDialog = false">Cancel</v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>

Additionally, there is another component named Home.vue

<template>
  <div>
      <Alert
      :alertDict="alert_dict"
      />  
  </div>
</template>
<script>
import Alert from "../components/Alert";
export default {
   methods: {
    ClickFunc: function () {
      this.alert_dict.showDialog = true
      if (User clicked Yes){
          console.log("Result")
      }
      Here I want to wait for users event to know which button is clicked by user.
      My Dialogbox is showing. 
    }
}


</script>

In this scenario, I am looking to capture the user's choice when interacting with the dialog box.

Answer №1

<template>
    <v-dialog v-if="alertDict" :value="alertDict.showDialog" max-width="460">
      <v-card>
        <v-card-title>Custom Title</v-card-title>
        <v-card-text>Custom Message</v-card-text>
        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn olcor="green darken-1" text @click="onClick('ok')">Ok</v-btn>
          <v-btn olcor="green darken-1" text @click="onClick('cancel')">Cancel</v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>
</template>
<script>
export default {
   methods: {
     onClick(value) {
       this.$emit('response', value);
     }
   }
}
</script>
<template>
  <div>
      <button @click="onClick">Show Dialog</button>
      <Alert
       :alertDict="alertDict"
       @response="onResponse"
      />  
  </div>
</template>
<script>
import Alert from "../components/Alert";
export default {
   data() {
     return {
       alertDict: {
         showDialog: false
       }
     }
   },
   methods: {
    onClick() {
      // Click function logic here
      this.alertDict.showDialog = true;
    },
    onResponse(value) {
      console.log(value);
      this.alertDict.showDialog = false;
    }
  }
}
</script>
<p>Here are some suggestions:</p>
<ol>
<li>Take time to study coding styles and choose one that resonates with you for improved code readability. Utilize camelCase for naming JavaScript functions and variables. Check out this helpful blog as a starting point:
<a href="https://medium.com/javascript-in-plain-english/javascript-naming-convention-best-practices-b2065694b7d" rel="nofollow noreferrer">https://medium.com/javascript-in-plain-english/javascript-naming-convention-best-practices-b2065694b7d</a></li>
<li>In your scenario, the <alert> component serves as a child component where you pass alertDict as props. Avoid directly mutating props within the child component (e.g., alertDict.showDialog=false) in Vue. Familiarize yourself with the concept of one-way data flow in Vue components by referring to:
<a href="https://v2.vuejs.org/v2/guide/components-props.html#One-Way-Data-Flow" rel="nofollow noreferrer">https://v2.vuejs.org/v2/guide/components-props.html#One-Way-Data-Flow</a>. Instead, emit events to the parent component and perform data mutations there.
Learn how to emit and handle events in Vue components effectively: 
<a href="https://v2.vuejs.org/v2/guide/components.html#Emitting-a-Value-With-an-Event" rel="nofollow noreferrer">https://v2.vuejs.org/v2/guide/components.html#Emitting-a-Value-With-an-Event</a></li>
</ol>
    </div></answer1>
<exanswer1><div class="answer accepted" i="63860479" l="4.0" c="1599903966" m="1657852913" a="TEhK" ai="13703967" e="dG9ueTE5" ei="6277151">
<pre><code><template>
    <v-dialog v-if="alertDict" :value="alertDict.showDialog" max-width="460">
      <v-card>
        <v-card-title>Title</v-card-title>
        <v-card-text>Message</v-card-text>
        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn olcor="green darken-1" text @click="onClick('ok')">Ok</v-btn>
          <v-btn olcor="green darken-1" text @click="onClick('cancel')">Cancel</v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>
</template>
<script>
export default {
   methods: {
     onClick(value) {
       this.$emit('response', value);
     }
   }
}
</script>
<template>
  <div>
      <button @click="onClick">show dialog</button>
      <Alert
       :alertDict="alertDict"
       @response="onResponse"
      />  
  </div>
</template>
<script>
import Alert from "../components/Alert";
export default {
   data() {
     return {
       alertDict: {
         showDialog: false
       }
     }
   },
   methods: {
    onClick() {
      // your ClickFunc
      this.alertDict.showDialog = true;
    },
    onResponse(value) {
      console.log(value);
      this.alertDict.showDialog = false;
    }
  }
}
</script>
<p>Some advice here,</p>
<ol>
<li>Educate yourself on coding style and choose one that suits you best, ensuring better code readability. Use camelcase for function and variable names in JavaScript script code. Here's a helpful blog to get started:
<a href="https://medium.com/javascript-in-plain-english/javascript-naming-convention-best-practices-b2065694b7d" rel="nofollow noreferrer">https://medium.com/javascript-in-plain-english/javascript-naming-convention-best-practices-b2065694b7d</a></li>
<li>In your example, the <alert> component is a child component where you pass alertDict as props. Mutating props directly in the child component (e.g., alertDict.showDialog=false) is not recommended in Vue. Check out the concept of one-way data flow in Vue components here:
<a href="https://v2.vuejs.org/v2/guide/components-props.html#One-Way-Data-Flow" rel="nofollow noreferrer">https://v2.vuejs.org/v2/guide/components-props.html#One-Way-Data-Flow</a>. Emit events to the parent component instead and mutate the data there.
Understand how to emit and handle events in Vue components: 
<a href="https://v2.vuejs.org/v2/guide/components.html#Emitting-a-Value-With-an-Event" rel="nofollow noreferrer">https://v2.vuejs.org/v2/guide/components.html#Emitting-a-Value-With-an-Event</a></li>
</ol>
    </div></answer1>
<exanswer1><div class="answer accepted" i="63860479" l="4.0" c="1599903966" m="1657852913" a="TEhK" ai="13703967" e="dG9ueTE5" ei="6277151">
<pre><code><template>
    <v-dialog v-if="alertDict" :value="alertDict.showDialog" max-width="460">
      <v-card>
        <v-card-title>Titile</v-card-title>
        <v-card-text>Message</v-card-text>
        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn olcor="green darken-1" text @click="onClick('ok')">Ok</v-btn>
          <v-btn olcor="green darken-1" text @click="onClick('cancel')">Cancel</v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>
</template>
<script>
export default {
   methods: {
     onClick(value) {
       this.$emit('response', value);
     }
   }
}
</script>
<template>
  <div>
      <button @click="onClick">show dialog</button>
      <Alert
       :alertDict="alertDict"
       @response="onResponse"
      />  
  </div>
</template>
<script>
import Alert from "../components/Alert";
export default {
   data() {
     return {
       alertDict: {
         showDialog: false
       }
     }
   },
   methods: {
    onClick() {
      // your ClickFunc
      this.alertDict.showDialog = true;
    },
    onResponse(value) {
      console.log(value);
      this.alertDict.showDialog = false;
    }
  }
}
</script>

Some suggestion here,

  1. Study some coding style, and find one which make sense to you and follow it, this is for better code readability. For example, naming convention, use camelcase in js script code for function and variable name.
    This is one of the blog u can study. https://medium.com/javascript-in-plain-english/javascript-naming-convention-best-practices-b2065694b7d
  2. In your example, <Alert> is child component, and you pass alertDict into <Alert>(child) as props. Then you mutate the props(alertDict.showDialog=false) in <Alert>(child).
    This is bad practice in vue. https://v2.vuejs.org/v2/guide/components-props.html#One-Way-Data-Flow
    Instead of mutate props, emit event to parent and mutate it in parent.
    Understand emit and event of vue component. https://v2.vuejs.org/v2/guide/components.html#Emitting-a-Value-With-an-Event

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 a function as an argument to handle the onClick event

I have a function that generates a React.ReactElement object. I need to provide this function with another function that will be triggered by an onClick event on a button. This is how I call the main function: this._createInjurySection1Drawer([{innerDra ...

Group By feature in AngularJS

Hey there, I'm trying to display a table from the code provided below. The issue I'm facing is with grouping by name. Here's how I want the view to appear: Shareholder | Preferred Stock | Common Stock | Options | Percentage | |----------- ...

Is there a way to manually add route resolve data to a controller without using automatic injection?

Two routes in my application share a controller, but one route requires data to be resolved before the view loads while the other does not. Here is an example of the routing segments: ... when('/users', { controller: 'UsersCtrl', ...

Ways to extract specific HTML from a jQuery element

When fetching html from a website, how can I extract specific html content instead of getting all of it? I have attempted the following method: Before appending data to the target below container.html(data); I would like to perform something like data.f ...

The error message indicates that the argument cannot be assigned to the parameter type 'AxiosRequestConfig'

I am working on a React app using Typescript, where I fetch a list of items from MongoDB. I want to implement the functionality to delete items from this list. The list is displayed in the app and each item has a corresponding delete button. However, when ...

Populating a ListBox without the need to constantly scroll upwards

I'm currently facing an issue with a ListBox that displays online users fetched from a MySQL database. Every second, the ListBox is updated with new users. The problem arises when adding an item to the ListBox causes it to scroll up, which I want to a ...

Converting a table into div elements and subsequently reverting it back to its original table format

[STOP DOWNVOTING: NEW AND IMPROVED] Discovering a simple technique on stackoverflow to transform tables into divs has been quite enlightening. By assigning classes to each tag, such as: <table class="table"> the process of converting from table to ...

Tips for incorporating PHP scripts into a Vue.js project during the production process

When using the CLI to generate a project with vue create project I am trying to figure out how to integrate PHP code into .Vue files without breaking the build command: npm run build For example, I want to add some <?php ?> code i ...

Transfer a JSON object to a Java Class without relying on a servlet

I have a form in HTML where I collect user input and store it as an object in JavaScript. Here is how I am creating the object: var dataObject = { Name: getName(), Age : getAge() } Now, I want to send this object using Ajax to a b ...

Create a JavaScript variable every few seconds and generate a JSON array of the variable whenever it is updated

My variable, which consists of random numbers generated by mathrandom every second, such as "14323121", needs to be saved to an array for the latest 10 updates. function EveryOneSec() { var numbers = Math.random(); // I want to create an array from th ...

Exploring the differences between two CSS style attributes using JQuery

Can someone help me with a quick question about CSS? I need to compare two style attributes, specifically margin-left. If the margin-left is less than 500px, I don't want to make any changes. However, if it's greater than 500px, I want to add ano ...

javascript implement a process to iteratively submit a form using ajax

I have a dynamic form with an unknown number of input fields that are not fixed. While searching for solutions, I came across jQuery ajax form submission which requires manually constructing the query string. In this scenario, the number of input fields ...

Can TypeScript automatically deduce keys from a changing object structure?

My goal here is to implement intellisense/autocomplete for an object created from an array, similar to an Action Creator for Redux. The array consists of strings (string[]) that can be transformed into an object with a specific shape { [string]: string }. ...

$set { "array.variable.value" :"newvalue"} utilize a different term besides "variable" or implement a new variable

When working with mongoose to store user data, one of the attributes is an array called items. In my additems.js file: const User = require('../models/User'); var array = user.items; array.indexOfObject = function (property, value) { ...

Generate menu options in real-time

I have developed a React single page application using the NASA Picture of the Day API. I am looking to incorporate a drawer feature that displays a history of previously shown images, but only showing their dates. However, I am unsure of how to dynamical ...

Exploring the nuances of context and application methodologies within NUXT

I've been working on implementing bugsnagClient and its notify method within the file plugins/axios.js. The code I have is located in plugins/bugsnag.js import Vue from "vue" import bugsnag from "@bugsnag/js" import bugsnagVue from "@bugsnag/plugin-v ...

Ensure that the line above is shorter in length compared to the following line

Is there a way to ensure that the previous line of text is always shorter than the next one, even if the length of the text is unknown? For example: Lorem ipsum dolor sit amet, consectetur adipiscing et libero posuere pellentesque. Vivamus quis nulla jus ...

What is a more efficient way to search and substitute text in NodeJS?

I am currently working on a task that involves reading a file, passing its contents and multiple arrays to a function, using regex to see if there are any matches, replacing them, and finally updating the file. The code I have put together may not be the ...

Getting a JWT token from Express to Angular using ngResource: A step-by-step guide

Currently, I am utilizing a jwt token for user registration validation. A unique URL is generated and sent to the user via email, which leads them to the authentication page. On the server side, the token is decoded and I need to transmit this JSON data to ...

Learn how to activate a JS native event listener using jQuery for the 'change' event

This question is unique from the one found at How to trigger jQuery change event in code. The focus here is on the interaction of jQuery with native event listeners, rather than jQuery event listeners. I am using addEventListener to bind a change event fo ...