Vue Toastification
Vue Toastification is a versatile and highly customizable library for displaying toast notifications in Vue applications. It provides a simple, intuitive API for creating transient, dismissible messages with extensive configuration options for appearance, positioning, and behavior. The library currently offers a stable version 1.x (e.g., 1.7.14) for Vue 2, and a significant rewrite in version 2.x (currently in release candidate phase) specifically for Vue 3, which fully embraces the Composition API and a global event listener model. This design allows for easier integration and usage outside of traditional Vue components. Vue Toastification is actively maintained, with a focus on delivering robust SSR support, advanced customization through custom components, and a modern, Composition API-first approach in its upcoming major release. It differentiates itself with features like swipe-to-close, programmatic control over toasts, and comprehensive styling capabilities.
Common errors
-
ReferenceError: HTMLElement is not defined
cause Attempting to use `vue-toastification` in an SSR environment (e.g., Nuxt) without proper client-side guarding.fixEnsure `vue-toastification` is configured to run only on the client side. In Nuxt, register it as a client-only plugin (e.g., `~/plugins/toast.client.ts`) or wrap components using toasts in `<ClientOnly>`. -
Named export 'useToast' not found
cause Incorrect import path for `useToast` in a Nuxt 3 project, or CommonJS module interop issues in ESM-only build environments.fixFor Nuxt 3, import `useToast` specifically from `vue-toastification/composition/nuxt`. In other ESM environments, ensure your build tool correctly handles named exports from the library. -
DEPRECATION WARNING: Using / for division outside of calc() is deprecated and will be removed in Dart Sass 2.0.0.
cause Your project's Sass configuration, or an older version of `vue-toastification`'s internal SCSS, uses the deprecated `/` operator for division.fixUpdate your Sass files to use `math.div()` for division operations, ensuring ` @use "sass:math";` is present. If the warning comes from `vue-toastification`'s internals, ensure you are on a recent version that has addressed this (v1.7.12, v2.0.0-rc.2 fixed internal usages). -
"default" is not exported by ".../node_modules/vue/dist/vue.runtime.esm-bundler.js", imported by ".../node_modules/vue-toastification/dist/esm/index.js".
cause Attempting to use `vue-toastification` v1.x in a Vue 3 project. `vue-toastification` v1.x expects a default export for Vue, which is not how Vue 3 bundles its ESM.fixUpgrade to `vue-toastification` v2.x (e.g., `npm install vue-toastification@next`) if you are using Vue 3. Version 1.x is strictly for Vue 2.
Warnings
- breaking Vue Toastification v2.x is a complete rewrite targeting Vue 3 exclusively. It is not compatible with Vue 2 applications. Attempting to use v1.x with Vue 3, or v2.x with Vue 2, will result in build errors or runtime failures.
- breaking The Sass division operator `/` is deprecated and will be removed in Dart Sass 2.0.0. If your project uses Sass and overrides `vue-toastification`'s SCSS variables (e.g., for custom themes), you might encounter deprecation warnings or build errors with newer Sass versions.
- gotcha Vue Toastification is primarily a client-side library and does not fully support Server-Side Rendering (SSR). Directly using `useToast` or global `$toast` instances during SSR can lead to `ReferenceError: HTMLElement is not defined` or `window is not defined` errors.
- gotcha In Nuxt 3, directly importing `useToast` from `vue-toastification` may work in development but fail in production builds due to ESM/CommonJS compatibility issues, resulting in 'Named export 'useToast' not found' errors.
- gotcha Using version 1.7.14 with Vue 2.7.0+ might cause issues with the Composition API implementation, as Vue 2.7.0+ now includes its own Composition API. This can lead to conflicts or unexpected behavior if both are attempting to use different Composition API contexts.
Install
-
npm install vue-toastification -
yarn add vue-toastification -
pnpm add vue-toastification
Imports
- Toast
const Toast = require('vue-toastification'); // CommonJS import is not officially supported and can lead to issues, especially in Vue 3/ESM environments.import Toast from 'vue-toastification'; import 'vue-toastification/dist/index.css'; // In Vue 3 main.ts createApp(App).use(Toast, { /* options */ }); // In Vue 2 main.js Vue.use(Toast, { /* options */ }); - useToast
import useToast from 'vue-toastification'; // Not a default export. const toast = require('vue-toastification').useToast; // CommonJS + named export issues. import { useToast } from 'vue-toastification/composition/nuxt'; // Use this specific path ONLY when in Nuxt Composition API context, otherwise use the main package path.import { useToast } from 'vue-toastification'; // Inside a Vue 3 component setup() or <script setup> const toast = useToast(); toast.success('Message'); - POSITION
import POSITION from 'vue-toastification'; // Not a default export.
import { POSITION } from 'vue-toastification'; // ... in plugin options app.use(Toast, { position: POSITION.TOP_RIGHT });
Quickstart
import { createApp, h } from 'vue';
import App from './App.vue';
import Toast, { POSITION, TYPE } from 'vue-toastification';
import 'vue-toastification/dist/index.css';
const app = createApp(App);
app.use(Toast, {
position: POSITION.BOTTOM_RIGHT,
timeout: 3000,
closeOnClick: true,
pauseOnFocusLoss: true,
pauseOnHover: true,
draggable: true,
draggablePercent: 0.6,
showCloseButtonOnHover: false,
hideProgressBar: false,
closeButton: 'button',
icon: true,
rtl: false,
maxToasts: 5,
newestOnTop: true,
});
app.mount('#app');
// Inside your App.vue or any other component's <script setup>
// <template>
// <div>
// <button @click="showSuccessToast">Show Success</button>
// <button @click="showErrorToast">Show Error</button>
// <button @click="showCustomComponentToast">Show Custom Component Toast</button>
// </div>
// </template>
// <script setup lang="ts">
// import { useToast } from 'vue-toastification';
// const toast = useToast();
// const showSuccessToast = () => {
// toast.success('Your changes have been saved!');
// };
// const showErrorToast = () => {
// toast.error('An error occurred. Please try again.');
// };
// const showCustomComponentToast = () => {
// const CustomToast = h('div', { class: 'my-custom-toast' }, [
// h('h4', 'Custom Title'),
// h('p', 'This toast is rendered from a custom component!'),
// h('button', { onClick: () => toast.clear() }, 'Clear All Toasts')
// ]);
// toast(CustomToast, {
// type: TYPE.INFO,
// timeout: false, // Make it persistent
// closeButton: false,
// });
// };
// </script>