Skip to content

Commit

Permalink
Fixed #535
Browse files Browse the repository at this point in the history
  • Loading branch information
cagataycivici committed Oct 10, 2020
1 parent 41b7d34 commit 4670fda
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
2 changes: 2 additions & 0 deletions exports/usetoast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use strict';
module.exports = require('./components/toast/useToast.js');
5 changes: 4 additions & 1 deletion src/components/toast/ToastService.js
Original file line number Diff line number Diff line change
@@ -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);
},
Expand All @@ -13,5 +14,7 @@ export default {
ToastEventBus.emit('remove-all-groups');
}
};
app.config.globalProperties.$toast = ToastService;
app.provide(PrimeVueToastSymbol, ToastService);
}
};
12 changes: 12 additions & 0 deletions src/components/toast/useToast.js
Original file line number Diff line number Diff line change
@@ -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;
}
35 changes: 25 additions & 10 deletions src/views/toast/ToastDoc.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,35 @@ import Toast from 'primevue/toast';

</code></pre>


<h5>Getting Started</h5>
<p>Ideal location of a Toast is the main application template so that it can be used by any component within the application.</p>
<p>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.</p>

<p>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 <i>add</i> method of the <b>$toast</b> property of the application.</p>
<h5>Options API</h5>
<p><i>$toast</i> is available as a property in the application instance.</p>
<pre v-code.script>
<code>
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});
}
}

</code></pre>

<h5>Composition API</h5>
<p>The toast instance can be injected with the <i>useToast</i> function.</p>
<pre v-code.script>
<code>
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});
}
})

</code></pre>

Expand Down Expand Up @@ -173,11 +193,6 @@ this.$toast.add({severity:'success', summary: 'Specific Message', group: 'mykey'

<h5>Clearing Messages</h5>
<p><i>removeGroup(group)</i> clears the messages for a specific Toast whereas <i>removeAllGroups()</i> method clears all messages.</p>
<pre v-code.script>
<code>
this.$toast.removeAllGroups();

</code></pre>

<h5>Properties</h5>
<div class="doc-tablewrapper">
Expand Down

0 comments on commit 4670fda

Please sign in to comment.