Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Vue Toast not showing?

I am trying to add a toast to my code as per below, however no toast message appears and I dont get any errors in the console. The code works however and the invite gets sent. I have used it like this in other files and the toast message appears so im uncertain as to why it wouldn’t appear now.

Main.js file where I am importing toast and toast service:

import Toast from 'primevue/toast';
import ToastService from 'primevue/toastservice';

const app = createApp(App);

app.component('Toast', Toast);
app.use(ToastService);

In my file using the toast once an invite is sent if successful I want to display the success message:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<template>
    <div class="main-container">
        <Button @click="sendInvites"/>
      </div>
    </div>
</template>

<script> 
  export default {
    data() {
      return {
      };
    },
    methods: {
      createToast() {
        get('CreateInvites', { invites: this.invites.filter((invite) => invite.email.length > 0) }).then((data) => {
          if (data.length > 0) {
            this.$toast.add({
              severity: 'error',
              summary: 'Unable to send some invites',
              detail: data
                })
                .join('\n'),
              life: 9000,
            });
          }
          else {
             this.$toast.add({
              severity: 'success',
              summary: 'Success!',
              life: 3000,
            });
          }
        });
      },
    },

>Solution :

The ideal location of a Toast is the main application template so that
it can be used by any component within the application.

So, you need to use the <Toast> component in your main file (App.vue) like this-

<template>
  <Toast />
<template>

It means the Toast component is mounted while App is loaded into the DOM and it is ready to be displayed upon any event’s trigger like you did-

this.$toast.add({
      severity: 'error',
      summary: 'Unable to send some invites',
      detail: data
          .map((detail) => {
              return `${detail.email}: ${detail.error}`;
           })
           .join('\n'),
           life: 9000,
 });

For more information, read here- https://primefaces.org/primevue/toast

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading