2

I have a vue.js app where I'm using PrimeVue for the UI. I want to have util functions for displaying toasts, but it doesn't seem to work. I have everything set up, but I'm getting the error 'No PrimeVue Toast provided!'.

    <template>
  <Toast />
  <div class="app-container">...

and the utils file:

//toastUtils.ts
import { useToast } from 'primevue/usetoast';
    
    export function showError(message: string) {
      const toast = useToast();
      toast.add({
        severity: 'error',
        summary: 'Error',
        detail: message,
      });
    }

I'm including it in the component where it's supposed to be used:

import { showError, showSuccess } from '../../utils/toastUtils';
...
showError('Failed to fetch sales channels.');

it was working when I had the useToast() method directly inside the component, but with these utility functions it doesn't work anymore. Why is that?

0

3 Answers 3

2

Instead of calling useToast() inside your utility function, call it in the component where the utility function is used, and then pass the toast instance to the utility function. For example:

Updated toastUtils.ts:

import { ToastServiceMethods } from 'primevue/usetoast';

export function showError(toast: ToastServiceMethods, message: string) {
  toast.add({
    severity: 'error',
    summary: 'Error',
    detail: message,
  });
}

export function showSuccess(toast: ToastServiceMethods, message: string) {
  toast.add({
    severity: 'success',
    summary: 'Success',
    detail: message,
  });
}

Component:

import { useToast } from 'primevue/usetoast';
import { showError, showSuccess } from '../../utils/toastUtils';

export default {
  setup() {
    const toast = useToast();

    const handleError = () => {
      showError(toast, 'Failed to fetch sales channels.');
    };

    return { handleError };
  },
};
Sign up to request clarification or add additional context in comments.

Comments

0

showError should be a part of custom composable:

export function useShowError() {
  const toast = useToast();

  return function (message: string) {
    toast.add(...);
  }
}

Which is called according to how the composables are used, i.e. in component setup:

<script setup>
const showError = useShowError();
...
showError('Failed to fetch sales channels.');
...

Also, PrimeVue toasts can be used regardless of whether they are used in a component or not, their implementation doesn't benefit from being a composable.

Comments

0

resolve.dedupe fixed my issue.

vite.config.js of my app:

export default defineConfig({
  ...
  resolve: {
    dedupe: [
      'primevue'
    ],
  }
})

It also solves this problem (variables missing).

1 Comment

Welcome. Thanks for contributing. Could you edit to include an explanation of how/why your code solves the OP'S question. Links to like documentation & additional info are appreciated, but you need to also embed pertinent info from the links into your post. Otherwise if the link becomes unavailable, vital knowledge to understand your answer gets lost, which would downgrade the quality of SO. Plus users shouldn't need to click around the internet to understand what the post is meant to convey. You can read about what makes good answers/questions in SO help section.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.