Skip to content

Instantly share code, notes, and snippets.

@crissilvaeng
Created May 28, 2021 19:28
Show Gist options
  • Save crissilvaeng/962b88e52246550409f4c351b990e1a4 to your computer and use it in GitHub Desktop.
Save crissilvaeng/962b88e52246550409f4c351b990e1a4 to your computer and use it in GitHub Desktop.
Nuxt – Notification Plugin
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