MeForma / vue-toaster

Vue.js toast notification plugin for vue 3
MIT License
194 stars 39 forks source link

Use in Composition API supported? #16

Open andgeno opened 3 years ago

andgeno commented 3 years ago

Hi, I tried to use your plugin with Vue 3 and TypeScript but could not make it work. I added a .d.ts file to make TypeScript ignore missing types and just be happy. In my mina.ts I added

import Toaster from '@meforma/vue-toaster';
// ...
app.use(Toaster);

In my App.ts I am using the Composition API with setup() like this:

export default defineComponent({
    setup(props, context): any {
       // ...
       // this.$toast('Hi there');
       // ...
    }
}

Unfortunately, this.$toast is not available because of the Composition API. I checked the docs and searched a lot on the internet but to no avail.

Any idea how to make it work?

andgeno commented 3 years ago

Well, I just found out how to use it. You have to provide and inject!

This is my main.ts:

app.use(Toaster).provide('toast', app.config.globalProperties.$toast);

This is my App.ts:

    mounted() {
        this.$toast.show('Toast from mounted()!');
    },
    setup(props, context): any {
        const toast: any = inject('toast');
        toast.show('Toast from setup()!');
    }
jprodrigues70 commented 3 years ago

Thank you!

atonamy commented 3 years ago

It must be added in documentation here https://vuejsexamples.com/vue-js-toast-notification-plugin-for-vue-3/

ezef commented 2 years ago

Thanks @andgeno !!