diff --git a/exports/usetoast.js b/exports/usetoast.js new file mode 100644 index 000000000..3221bdaa2 --- /dev/null +++ b/exports/usetoast.js @@ -0,0 +1,2 @@ +'use strict'; +module.exports = require('./components/toast/useToast.js'); \ No newline at end of file diff --git a/src/components/toast/ToastService.js b/src/components/toast/ToastService.js index 70d273cef..860e8d3ad 100755 --- a/src/components/toast/ToastService.js +++ b/src/components/toast/ToastService.js @@ -1,8 +1,9 @@ import ToastEventBus from './ToastEventBus'; +import {PrimeVueToastSymbol} from './useToast'; export default { install: (app) => { - app.config.globalProperties.$toast = { + const ToastService = { add: (message) => { ToastEventBus.emit('add', message); }, @@ -13,5 +14,7 @@ export default { ToastEventBus.emit('remove-all-groups'); } }; + app.config.globalProperties.$toast = ToastService; + app.provide(PrimeVueToastSymbol, ToastService); } }; \ No newline at end of file diff --git a/src/components/toast/useToast.js b/src/components/toast/useToast.js new file mode 100644 index 000000000..13d019121 --- /dev/null +++ b/src/components/toast/useToast.js @@ -0,0 +1,12 @@ +import { inject } from 'vue'; + +export const PrimeVueToastSymbol = Symbol(); + +export function useToast() { + const PrimeVueToast = inject(PrimeVueToastSymbol); + if (!PrimeVueToast) { + throw new Error('No PrimeVue Toast provided!'); + } + + return PrimeVueToast; +} \ No newline at end of file diff --git a/src/views/toast/ToastDoc.vue b/src/views/toast/ToastDoc.vue index 5a895d577..7784dec70 100755 --- a/src/views/toast/ToastDoc.vue +++ b/src/views/toast/ToastDoc.vue @@ -30,15 +30,35 @@ import Toast from 'primevue/toast'; -
Ideal location of a Toast is the main application template so that it can be used by any component within the application.
+Ideal location of a Toast is the main application template so that it can be used by any component within the application. A single message is represented by the Message interface in PrimeVue that defines various properties such as severity, + summary and detail.
-A single message is represented by the Message interface in PrimeVue that defines various properties such as severity, - summary and detail. Messages are displayed by using the add method of the $toast property of the application.
+$toast is available as a property in the application instance.
-this.$toast.add({severity:'success', summary: 'Success Message', detail:'Order submitted', life: 3000});
+export default {
+ mounted() {
+ this.$toast.add({severity:'success', summary: 'Success Message', detail:'Order submitted', life: 3000});
+ }
+}
+
+
+
+ The toast instance can be injected with the useToast function.
+
+
+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});
+ }
+})
@@ -173,11 +193,6 @@ this.$toast.add({severity:'success', summary: 'Specific Message', group: 'mykey'
removeGroup(group) clears the messages for a specific Toast whereas removeAllGroups() method clears all messages.
-
-
-this.$toast.removeAllGroups();
-
-