-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ToastService $toast is not accessible using Composition API #535
Comments
Thanks, added useToast; import { defineComponent } from "vue";
import { useToast } from "primevue/usetoast";
export default defineComponent({
setup() {
const toast = useToast();
toast.add({severity:'info', summary: 'Info Message', detail:'Message Content', life: 3000});
}
}) |
@cagataycivici
|
In my app [email protected] it is still not working, same behaviour as @mateuszgiza described. Is there a change that it will be fixed soon? |
Having the same issue. Workaround is to add
|
still not working.. |
you can it use like this: setup() {
onMounted(() => {
console.log('mounted')
const toast = useToast()
console.log(`toast: `, toast)
toast.add({
severity:'info',
summary: 'info summary',
detail:'info detail',
life: 3000
})
})
} |
How to add PrimeVue Toast to Vue 3 Unit testing? //------------------------------------------------------------- |
Vue3 + Typescript (composition API) Toast is not working
Not working with composition API (Vue3 + TS) "vue": "^3.1.5", want to use toast as alert for max allowed number |
Same here doesn't work |
Did you add the ToastService?
|
This is still not working |
"primevue": "^3.12.2", Dont work ! |
Please try; |
Its odd, i tried all of the above with composition API and Vue/Vite, only the solution by @frasza worked. I am confused. |
This might be happening because internally, "useToast" function uses provide/inject. And as you may know from Vue's documentation, you can use these functions only inside Components, not functions. (This is only my intuition, did not check the code for the function "useToast") Is there any way we could avoid using provide/inject, so we could get around this limitation and use "useToast" anywhere we want? |
I tried everything mentionned in this thread and nothing works on 3.21.0 |
Thanks you @frasza I was facing the same issue with DynamicModal (https://primevue.org/dynamicdialog) So puting the |
Everything as mention above not working at my end. |
Frankly, though PrimeVue is great, its too much work of importing and configuring just get some UI components. Rather use others like Vuetify, As solution developers, we found the users dont really care too much about the looks of a page. They are more concerned with the feel and functionalities. Take the airlines industry for example - still using teletype interface and PC hardware dealers using the old Dbase software. Perhaps i am reconsider using PrimeVue when it gets simpler. |
The solution to this problem is very simple. Vue instance inside nuxt needs to know where is the runtime core service functions related to toast, dialog, confirm and so on. export default defineNuxtPlugin(nuxtApp => {
const { vueApp, provide } = nuxtApp
vueApp.use(PrimeVue, { ripple: true })
vueApp.use(ToastService)
vueApp.use(DialogService)
vueApp.use(ConfirmationService)
vueApp.component('Toast', Toast)
vueApp.component('DynamicDialog', DynamicDialog)
vueApp.component('Dialog', Dialog)
vueApp.component('ConfirmDialog', ConfirmDialog)
provide('toast', vueApp.config.globalProperties.$toast)
provide('dialog', vueApp.config.globalProperties.$dialog)
provide('confirm', vueApp.config.globalProperties.$confirm)
}) You can use without injection error on your component using primevue composables (wich provide TS types): <template>
<div>
<Toast />
</div>
</template>
<script setup lang="ts">
import { useToast } from 'primevue/usetoast'
const toast = useToast()
onMounted(() => {
toast.add({
severity: 'warn',
summary: 'Hello',
detail: 'World',
life: 3000
})
})
</script> Related to: #2533 |
What frightens me is the number of imports and exports just to get some UI components. I would decided on using this product when things are simplified like Vuetify or Bootstrap. Its a very good product but too much "configuration". |
I wanted to highlight toast.add in a composables component and encountered a similar error - const newToast = () => { useToast().add() };
const TOAST = useToast();
const newToast = () => { TOAST.add() };
// or using `function` instead const
function dialog() { useToast().add() } UPDATE: ok, I think I made a mistake in the explanation. Perhaps this is due to "hoisting". |
In documentation in order to use toast we have to access it on like this
Since there is no this in setup() method of Composition API I can't access it like that:
I currently solved this by using my wrapper around ToastService registration but it would be nice to get it into library and provide us with some function like useToastService that would inject the service.
My workaround:
I can submit some PR if you are interested.
The text was updated successfully, but these errors were encountered: