javascriptvue.jsvue-sweetalert2

How to pass a user input and update the data using Vue-SweetAlert2


I'm currently trying to make a "create" button that triggers a popup with a user text input as the attached image below.

popup

If it works as I intend, it should store the name written in the text field when the OK button is clicked, and then the name should be reflected on the page like below, as I typed my own name 'Shinichi' as the input.

Test result of createCustomer: Shinichi

But in reality, what is displayed is the hardcoded Object which is defined in the alertDisplay() method in the code below.

Test result of createCustomer: { "params": { "title": "What is your Name?", "input": "text", "inputPlaceholder": "Enter your name here", "showCloseButton": true } }

ObjectDisplayed

<template>
      <v-btn class="create-button" color="yellow" @click="alertDisplay">Create</v-btn>

    <br/>

    <p>Test result of createCustomer: {{ createdCustomer }}</p>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        createdCustomer: null
      }
    },
    methods: {
      alertDisplay() {
        var customer = this.$swal({
                          title: 'What is your Name?',
                          input: 'text',
                          inputPlaceholder: 'Enter your name here',
                          showCloseButton: true,       
                        });
        console.log(customer);
        this.createdCustomer = customer; 
      }
    }
  }
</script>

How can I pass the name and have it displayed on the screen instead of the Object like in the attached image?


Solution

  • You have to use promises to get the user input of a swal modal.

    async alertDisplay() {
        let customer = await this.$swal({
            title: 'What is your Name?',
            input: 'text',
            inputPlaceholder: 'Enter your name here',
            showCloseButton: true,       
        });
        console.log(customer)
    }
    

    You can see it in this Codepen found on the documentation : https://codepen.io/pen/?&editable=true