Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Snackbar Vuetify - override method after timeout

I want to ask You how I can define a method which execute after the timeout? After that timeout i want to execute $emit event, but I don't know how can i do this...

<v-snackbar
  v-model="snackbar"
  :color="primary"
  :timeout="5000"
>
  {{ text }}
  <v-btn
    dark
    flat
    @click="snackbar = false"
  >
    Close
  </v-btn>
</v-snackbar>

https://vuetifyjs.com/en/components/snackbars

like image 230
Mear Elin Avatar asked Oct 30 '18 21:10

Mear Elin


People also ask

How do I create a snackbar in vuetify?

We can create snackbars in Vuetify using the v-snackbar component. In the code below, we create a "Close" button in the action slot of the snackbar. When this button is clicked, the snackbar variable is set to false to hide the snackbar. snackbar is false by default, so at first we can only see the button that opens the snackbar:

What is the timeout property in the V-snackbar?

The timeout property lets you customize the delay before the v-snackbar is hidden. Apply different styles to the snackbar using props such as text, shaped, outlined, and more.

What is a snackbar and how to use it?

A snackbar helps to display quick messages. We can use it to notify users of certain events that occur in an app (for example, deleting an item from a list). They might also contain actions related to the information shown that users can take. In this article, we’re going to learn how to create snackbars using the Vuetify framework.

What is the multi-line property of a snackbar?

The multi-line property extends the height of the v-snackbar to give you a little more room for content. The timeout property lets you customize the delay before the v-snackbar is hidden. Apply different styles to the snackbar using props such as text, shaped, outlined, and more.


2 Answers

According to the documentation there's no event attached to that property, but i will give a solution that responds to your use case, add timeout property to your data object as follows :

   data() {
         return {
          snackbar:false,
          timeout:6000,
          ....
         }
    }

add an event handler to your button click :

     <v-btn block
       color="primary" 
       dark
       @click="showSnackbar">
       Show Snackbar
    </v-btn>

in your methods add showSnackbar method

    methods: {
         showSnackbar() {
           this.snackbar=true;
           setTimeout(() => { this.$emit("yourEvent"); },this.timeout);
         }
       }

I simulate a your case in this pen

like image 78
Boussadjra Brahim Avatar answered Sep 21 '22 15:09

Boussadjra Brahim


You could also use a watcher. Watch for snackbar === false, then execute the function.

like image 31
Skye Avatar answered Sep 17 '22 15:09

Skye