modbender / nuxt-snackbar

A Nuxt Snackbar/Toast Implementation independent of CSS framework
https://nuxt.com/modules/snackbar
MIT License
26 stars 6 forks source link

Error: No Snackbar provided! #4

Closed autorunman22 closed 1 year ago

autorunman22 commented 1 year ago

I wrap the snackbar calls to simple composable so that I can reduce boilerplate:

composables/useToast.js

export const useToast = (type: string = "success", title: string = "", text: string) => {
    const snackbar = useSnackbar();

    snackbar.add({
        type: type,
        title: title,
        text: text
    })
}

But when I refresh the page, i'm getting this error:

Uncaught (in promise) Error: No Snackbar provided!
    at be (vue3-snackbar.js?v=80f43774:407:11)
    at useToast (useToast.ts:3:5)
modbender commented 1 year ago

For now as I see it, it won't work because the useSnackbar needs to be called inside setup. I'll check more on it. @craigrileyuk if you could help out...

AnttiMJohansson commented 1 year ago

I've followed the steps from readme and end up with same error: Uncaught Error: No Snackbar provided!

In my case the gets highlighted as not recognized: (property) NuxtSnackbar: typeof _default Type '{}' is not assignable to type 'ComponentProps'. Type '{}' is missing the following properties from type 'typeof _default': render, injectts(2322)

AnttiMJohansson commented 1 year ago

Sorry the code that gets highlighted got dropped from message.

<NuxtSnackbar />

craigrileyuk commented 1 year ago

@autorunman22

Inside your useToast composable, try wrapping your main function in an onMounted a la

onMounted(() => {
    snackbar.add({
        type,
        title,
        text,
    });
});

I've used the exact same setup as this and it's working in my test app.

autorunman22 commented 1 year ago

I can confirm that its working. Thank you @craigrileyuk