"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

I have chosen not to rely on Media Query for ensuring responsiveness in my Angular 2 application, and instead opted to utilize JavaScript. Do you think this approach is considered a

I am in the process of ensuring that my app is fully responsive across all screen sizes. Instead of relying on Media Query, I have chosen to use JavaScript with Angular 2 to achieve this responsiveness, utilizing features such as [style.width], *ngIf="is ...

The input type '{}' does not match the expected type 'Readonly<IIdeasContainerProps>'. The property '...' is not found in the type '{}'

Having recently started using TypeScript, I'm encountering some issues when attempting to execute this code snippet. Error The error message reads as follows: Failed to compile 13,8): Type '{}' is not assignable to type 'Readonly &l ...

Unexpectedly, the React custom component is failing to render as intended

I am anticipating the text "SMALL Medium, Big" to be displayed on the screen but unfortunately it's not showing up function Box(prop){ const ele = <div className={prop.cn}> </div> return ele } ...

Switching colors after uncovering text using JavaScript

I'm striving to achieve specific results with my code, but I'm having trouble pinpointing what exactly I'm doing wrong. Any guidance on how to correct my approach would be greatly appreciated. Currently, I've created rows that can be c ...

Create a data attribute object and assign to it the prop object received from the parent component

I am struggling with passing an object as a prop from a parent component and then utilizing it to initialize the child component with the received value. The main objective behind this is to create a dialog box that includes a child form component with mu ...

Error Encountered during Deployment of Custom React App on Heroku due to Fetch API Issue

After developing a small app using React without CRA, I successfully deployed it to Heroku. However, I encountered an issue where a static JSON file that I created isn't fetching properly (it works fine on my local machine). Upon encountering this pr ...

Tips for aligning a cluster of floating buttons at the center in Vuetify:

This is the code I am currently working with: <v-container height="0"> <v-row align="center" justify="center"> <v-hover v-slot:default="{ hover }" v-for="(option, index) in options" ...

Give it a little time before uploading a widget onto the page

As a newcomer to programming, I recently came across this code from an open source project. I am in the process of loading a widget onto a website. Instead of having the widget load instantly, I would like it to wait 10 seconds before displaying. Below i ...

Issue with ExpressJS Regex not correctly matching a path

I'm currently struggling with a simple regex that is supposed to match words consisting of letters (0-5) only, but for some reason it's not working as expected. Can anyone help me figure out the correct expression and how to implement it in Expre ...

Is it necessary to ensure application readiness before proceeding with unit testing exports?

I've been facing a challenge while trying to utilize Jest for unit testing an express API that I've developed. The issue arises when the database needs to be ready before running the test, which doesn't seem to happen seamlessly. In my serve ...

React - utilizing dynamic properties using string values

I am facing a challenge in my test suite where I need to generate components with dynamic props. The desired components should look like this: <Button primary /> <Button secondary /> However, I am currently stuck at this point: [ &apos ...

Error: JSON key data not present in rendering

Here is a JSON string I am working with: {"{\"nodeName\":\"abc\"}":[{"url":"abc","status":true},{"url":"abc","status":true}]," {\"nodeName\":\"pqr\"}":[{"url":"abc","status":true},{"url":"abc","status":true}]} ...

Once the object was placed in the array, it no longer exhibits reactivity

Why did the element's reactive property turn into a non-reactive one after passing through the reactive array? Can you please explain this phenomenon? <script setup lang="ts"> import { ref, Ref } from 'vue' inter ...

How can I determine the size of the custom options dropdown in Magento?

I'm facing a challenging issue that I can't seem to crack. It might be because I'm still learning the ropes and struggling with technical jargon. Please bear with me as I try to explain my problem. Here is a summary of what I'm trying ...

Invalid web address entered

While attempting to incorporate a functionality that opens a new window to a specific link when clicking an icon, I encountered an issue where the click action redirects to the wrong URL. Instead of directing to the URL specified in its href attribute, it ...

Implement a FOR loop in conjunction with an AJAX request

My goal is to send an AJAX request with multiple form fields and use an array for validations. I would like to also pass these values via AJAX: Although I haven't used a for loop in JavaScript before, it looks familiar. The current approach of the l ...

How to extract information from a JavaScript object array in Node.js?

Currently, I am attempting to extract data from a JavaScript Object array by providing field names and then storing the data in an array. However, my current approach seems to be yielding incorrect results. Whenever I try to log the values stored in ' ...

Activate fullscreen mode in Krpano on desktop by clicking a button

Is there a way to activate fullscreen mode upon clicking a button? I believe I should use the code: krpano.set(fullscreen,true); Currently, I have an image within a slideshow that includes a play button overlay. Once the button is clicked, the slideshow ...

Having trouble inputting text into the text area using Selenium WebDriver with C#

Here is the original code snippet: I am having trouble entering text in the text box below. Can you please help me figure out how to enter text in this text box? <td id="ctl00_cRight_ucSMS_redSMSBodyCenter" class="reContentCell" ...

React: troubleshooting error of empty object displayed by console log

Just diving into the world of React and facing a challenge with importing a list of objects from a JS file to set them as the initial state of my app. Here's what I've tried: import allSamples from './reducers/reducerSamples'; var App ...