Vue SweetAlert2 Wrapper

5.0.11 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to install and configure `vue-sweetalert2` in a Vue 3 application, including setting global SweetAlert2 options and examples of triggering different types of alerts from a component using the injected `this.$swal` method.

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>

view raw JSON →