Created
May 28, 2021 19:28
-
-
Save crissilvaeng/962b88e52246550409f4c351b990e1a4 to your computer and use it in GitHub Desktop.
Nuxt – Notification Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Notification from '@/components/Notification' | |
import Vue from 'vue' | |
const NotificationClass = Vue.extend(Notification) | |
export default ({ app }, inject) => { | |
inject('notification', { | |
show({ ctx, title, message = '', type = 'info' }) { | |
const notification = new NotificationClass({ | |
propsData: { title, message, type }, | |
parent: ctx, | |
}) | |
notification.$mount() | |
ctx.$el.appendChild(notification.$el) | |
const destroy = () => { | |
ctx.$el.removeChild(notification.$el) | |
notification.$destroy() | |
} | |
notification.$on('close', () => { | |
destroy() | |
}) | |
return { destroy } | |
}, | |
success({ ctx, title = 'Success', message = '' }) { | |
return this.show({ ctx, title, message, type: 'success' }) | |
}, | |
warn({ ctx, title = 'Warning', message = '' }) { | |
return this.show({ ctx, title, message, type: 'warn' }) | |
}, | |
error({ ctx, title = 'Error', message = '' }) { | |
return this.show({ ctx, title, message, type: 'error' }) | |
}, | |
info({ ctx, title = 'Information', message = '' }) { | |
return this.show({ ctx, title, message, type: 'info' }) | |
}, | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment