Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReferenceError: "Swal is not defined"

I'm working on small project using codeigniter and VueJs and sweet alert javascript library. but i get an error in my console ReferenceError: "Swal is not defined" once i call swall.fire({}) inside my VueJs methods. This is my code :

deleteCustomers(customer_id){
        Swal.fire({
           title: 'Are you sure?',
           text: "You won't be able to revert this!",
           icon: 'warning',
           showCancelButton: true,
           confirmButtonColor: '#3085d6',
           cancelButtonColor: '#d33',
           confirmButtonText: 'Yes, delete it!'
           }).then((result) => {
              axios.post('/Thirdparty/deleteCustomers', {id : customer_id}).then(function(response){
                 console.log(response.data);
                 //alert(response.data.error);
                 Swal.fire(
                 'Deleted!',
                 'Your file has been deleted.',
                 'success'
                 );
              });
           });
     }

and of course i have already imported swall using : import Swal from 'sweetalert2';

NB : swall.fire doesn't work only inside Vuejs methods

like image 989
Amine Bouhaddi Avatar asked Nov 16 '19 15:11

Amine Bouhaddi


1 Answers

Here is a quick way to implement Sweet Alert on vue js

npm install sweetalert2

on your app.js register sweet alert to make it accessible in all your components

import swal from 'sweetalert2';
window.Swal = swal;

Then in any of the components of example ( Example.vue)

<template>
  ...
</template>
<script>
   export default{
     methods:{
       test(){
         Swal.fire('Test!', 'Hello test message','success');
      } 
    }
   }

More Sweet alert methods can be found here

like image 169
Felix Runye Avatar answered Oct 20 '22 01:10

Felix Runye