Vue SweetAlert2 Wrapper
vue-sweetalert2 is a Vue.js wrapper for the popular SweetAlert2 library, providing declarative integration of customizable alert, prompt, and confirmation dialogs into Vue applications. It supports both Vue 2 and Vue 3 (Options API) and offers server-side rendering (SSR) capabilities, especially through its Nuxt.js module. The current stable version is 5.0.11. While the project is actively maintained with periodic updates addressing bug fixes and minor features, there's no strict release cadence. A key differentiator is its `this.$swal` instance method, simplifying SweetAlert2 calls within Vue components. However, for Vue 3 Composition API users, the maintainer explicitly recommends against using this wrapper, suggesting direct SweetAlert2 calls for better feedback and alignment with its documentation.
Common errors
-
Property '$swal' does not exist on type 'ComponentPublicInstance'.
cause The `vue-sweetalert2` plugin was not correctly installed using `app.use()` in your Vue application's entry point.fixEnsure `app.use(VueSweetalert2);` is called before `app.mount('#app');` in your main application file (e.g., `main.ts` or `main.js`). -
SweetAlert2: The parameter is expected to be of type Object, but got type String.
cause Passing a string directly to `this.$swal()` or `this.$swal.fire()` without wrapping it in an object. SweetAlert2's modern API expects a configuration object as its primary parameter.fixAlways pass a configuration object to SweetAlert2 methods, even for simple alerts. For example, use `this.$swal({ title: 'Hello', text: 'World!' });` instead of `this.$swal('Hello world!');`. -
SweetAlert2 modal appears without any styling or looks broken.
cause The core SweetAlert2 CSS file (`sweetalert2/dist/sweetalert2.min.css`) is not being loaded or is being overridden by conflicting styles. This can happen due to the `Prevent auto import of sweetalert style` feature in `v4.2.0+` or incorrect Nuxt.js configuration.fixVerify that `import 'sweetalert2/dist/sweetalert2.min.css';` is present in your application's entry file. If using Nuxt.js, check your `nuxt.config.js` for proper module configuration (e.g., `sweetalert` options or `no-css` module combined with manual CSS import).
Warnings
- gotcha The package maintainer explicitly recommends against using `vue-sweetalert2` with Vue 3 Composition API. It is advised to import and use `sweetalert2` directly for better integration feedback and alignment with its native documentation when using Composition API.
- breaking Version `3.0.5` introduced a 'Disable force css including' feature, which was subsequently reverted in `3.0.6`. This could lead to inconsistent CSS loading behavior if not explicitly handled, potentially causing SweetAlert2 modals to render without styling in specific build setups.
- gotcha Starting from `v4.2.0`, a feature to 'Prevent auto import of sweetalert style' was added. This means the `sweetalert2.min.css` might no longer be automatically included by the plugin, which can result in unstyled modals.
- gotcha When integrating `vue-sweetalert2` with Nuxt.js, specific module paths must be used, such as `vue-sweetalert2/nuxt` or `vue-sweetalert2/nuxt/no-css`. Using the incorrect or generic package path can lead to improper injection of `$swal` or missing styles.
Install
-
npm install vue-sweetalert2 -
yarn add vue-sweetalert2 -
pnpm add vue-sweetalert2
Imports
- VueSweetalert2
const VueSweetalert2 = require('vue-sweetalert2');import VueSweetalert2 from 'vue-sweetalert2';
- sweetalert2/dist/sweetalert2.min.css
import 'sweetalert2/dist/sweetalert2.min.css';
- $swal
import { $swal } from 'vue-sweetalert2';this.$swal('Hello Vue world!!!');
Quickstart
import { createApp } from 'vue';
import App from './App.vue';
import VueSweetalert2 from 'vue-sweetalert2';
import 'sweetalert2/dist/sweetalert2.min.css'; // Essential for default styling
const app = createApp(App);
// Optional: Configure global SweetAlert2 options
const options = {
confirmButtonColor: '#41b882',
cancelButtonColor: '#ff7674',
};
app.use(VueSweetalert2, options);
app.mount('#app');
// App.vue (example component)
<template>
<div>
<h1>Vue SweetAlert2 Demo</h1>
<button @click="showSuccessAlert">Show Success Alert</button>
<button @click="showErrorPrompt">Show Error Prompt</button>
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
name: 'App',
methods: {
async showSuccessAlert() {
await this.$swal({
title: 'Operation Successful!',
text: 'Your action was completed successfully.',
icon: 'success',
confirmButtonText: 'OK',
});
},
async showErrorPrompt() {
const { value: text } = await this.$swal.fire({
title: 'Why did the error occur?',
input: 'textarea',
inputPlaceholder: 'Type your reason here...',
inputAttributes: {
'aria-label': 'Type your reason here'
},
showCancelButton: true,
});
if (text) {
this.$swal.fire('Reason provided:', text);
}
},
},
});
</script>