Toastification is a lightweight, configurable, pretty nice toast notification library for Vue.js apps.
# Yarn
$ yarn add vue-toastification
# NPM
$ npm install vue-toastification --save
Add it as a Vue plugin:
import Vue from "vue";
import Toast from "vue-toastification";
// Import the CSS or use your own!
import "vue-toastification/dist/index.css";
const options = {
// You can set your default options here
};
Vue.use(Toast, options);
And then just call it in your components with
this.$toast("I'm a toast!");
// Or with options
this.$toast("My toast content", {
timeout: 2000,
onClose: () => console.log("closed!")
});
// These options will override the options defined in the "Vue.use" plugin registration for this specific toast
Or reference in your Vuex store with
this._vm.$toast("I'm a toast!");
// Or with import
import Vue from "vue";
Vue.$toast("My toast content", {
timeout: 2000,
onClose: () => console.log("closed!")
});
By default, the toasts will be displayed at the top right corner of your screen, but you can set it manually using the position option.
The following values are allowed: top-right, top-center, top-left, bottom-right, bottom-center, bottom-left.
Vue.use(Toast, {
// Setting the global default position
position: "top-left"
});
// Or set it per toast
this.$toast("I'm a toast", { position: "bottom-left" });
Depending on the context, you may want to use toasts of different colors. You can easily do that by setting the type of toast to be displayed.
this.$toast("Default toast");
this.$toast.info("Info toast");
this.$toast.success("Success toast");
this.$toast.error("Error toast");
this.$toast.warning("Warning toast");
// You can also set the type programmatically when calling the default toast
this.$toast("Also a success toast", {
type: "success" // or "error", "default", "info" and "warning"
});
Setting the type only works when using this.$toast, it wonβt work when registering the plugin with Vue.use.
You can set for how long the toast will remain on the screen (in milliseconds) using the timeout
option, or disable it altogether by setting it to false
// 1 second
this.$toast("Quick toast", { timeout: 1000 });
// Or make the toast permanent until manually closed
this.$toast("Persistent toast", { timeout: false })
// Or set it when registering the plugin
Vue.use(Toast, { timeout: 2000 });
Author: Maronato
Live Demo: https://maronato.github.io/vue-toastification/
Download Link: https://github.com/Maronato/vue-toastification/archive/master.zip
Official Website: https://github.com/Maronato/vue-toastification
#vuejs #javascript #vue-js