basic usage

2 - Add the component that renders the notifications. You probably want to put this somewhere in the root component of your site, so it can always be visible. There is a simple component (named notivuecation) available, but you can also create and use your own.

<notivuecation />

3 - Call $alert or $confirm from any Vue component:

this.$alert('You are the 1 millionth visitor!').then(claimPrize);
this.$confirm('Are you sure?').then(result => {
// result is true/false
});

overview

The plugin adds three methods to the Vue instance, these can be called from any Vue component:

$notify

$confirm

$alert

If you want to use these methods from outside a component, you can also import them:

import { confirm, alert, notify } from 'notivuecation';

(this will obviously replace the native confirm and alert methods in the current scope)

notify

The notify method is the base of the plugin and is internally always used to show any notification. It accepts an object that defines the title, a message and all buttons to show:

The optional css property on the buttons will be set as css class on the button, while the value (also optional) will be used when resolving the promise (i.e. this is the value that will end up in the then when clicking the button).

confirm and alert

confirm and alert are shorthand methods that call notify with some predefined data to cover most usecases. Use these methods to show either a notification with Ok/Cancel buttons or just a single Ok-button. Both accept roughly the same parameters object:

this.$confirm({
title: 'Warning!', // default is either 'Confirm' or 'Alert'
message: 'Please confirm that you have read this.',
confirm: 'Sure man', // default is 'Ok'
cancel: 'No way', // default is 'Cancel', not used for $alert
}).then(result => {
// result is true/false when using $confirm, and not set for $alert
});

Both methods can also be called with a string as argument, which is the same as using an object with only the message property set. So these two are equal:

When using confirm or alert, all confirm buttons will have the css-class confirm and all cancel buttons cancel.

styling

The default component has some basic styling to overlay the content and show the message.
For your own project you might want to use custom styling for the notification itself.
There are some basic styles and simple classes which you can override easily.
A small example when using CSS Modules (with SCSS) in your Vue project.

Which would make the notification mask lighter, the content background black and the content contrasting white.

custom component

If you want to use your own component for displaying the notification, just add the componentMixin to your component's mixins. It will add the following properties to the component: title, buttons, message, type, resolve and notification.

options

When initializing the plugin, you can supply an object as second argument to set some options.

Vue.use(notivuecation, {
someOption: 'someValue',
});

The available options are:

addMethodsToVue: Whether the notify, alert and confirm methods should be added to the Vue instance. Default: true.

componentName: Name of the default component. Default: 'notivuecation' . This defines the element name you should use for the component: <notivuecation />

getButtonForEscape: Can make the escape-key close a notification in certain cases. A function should be set (which receives the notification-data as argument) that returns the button that will be used when pressing the escape key. The default will make the key work in two cases: the cancel button for a confirm call, or the single button when there is only one. If you don't want the escape key to do anything, you can set the property to null.