" /> " /> "/>

Is it possible to use Vue-sweetalert2 html as vue component?

1.4k Views Asked by At

I use this package https://www.npmjs.com/package/vue-sweetalert2 and I want to past vue component in Vue.swal as html.

<template>
    <div class="w-full">
        <div class="py-6 w-full">
            <button
                type="button"
                class="btn btn-default btn-primary"
                @click="openTeamsModal"
            >
                Teams
            </button>
        </div>
    </div>
</template>

<script>

import Vue from 'vue';
import TeamTable from "./TeamTable";
import VueSweetalert2 from 'vue-sweetalert2'; 
Vue.use(VueSweetalert2);

export default {
    components: {TeamTable},

    props: [
        'resourceName',
        'resourceId',
        'field'
    ],

    data: () => ({
        teams: [],
    }),

    methods: {

        openTeamsModal() {

            Nova.request().get(`/api/competitors/${this.field.seasonId}/${this.field.championshipId}`).then( responce  => {
                console.log(responce)
            });

            Vue.swal({
                title: 'Test',
                html: '<team-table></team-table>',
            });
        }

    },
}
</script>

but there is nothing. I am new to VueJs and I still don’t fully understand how to insert my components as html.

1

There are 1 best solutions below

2
Alejandro De Castro On

Yes! is possible! ... after hours of research, I have succeeded.

this is the way:

you need to include all the logic of the template between this characters ` `

you will also need edit the vue.config.js file and add inside the configurewebpack object add this: 'vue$':'vue/dist/vue.esm.js'

configureWebpack: {
  resolve: {
    alias: {
      'src': resolveSrc('src'),
      'chart.js': 'chart.js/dist/Chart.js',

      // add this line for include components inside swal alert
      'vue$':'vue/dist/vue.esm.js'
    }

Once this is done you must relaunch the project "npm run dev"

This is my complete example, tested and working

openSweet() {
      Vue.component('my-comp', {
          template: `
                <div class="card-content">
                  <div class="span2">
                        <div class="col-sm-6 col-md-2 col-lg-3">
                            <div class="row">
                              <div style="margin-top: 6px;" >
                                <p-switch v-model="switchTrip.state" type="primary" on-text="ON" off-text="OFF" style="justify-content:center"></p-switch>
                                <h5 class="card-title" style="margin-left: 25px;">Recorridos</h5>
                              </div>
                            </div>

                            <div class="row">
                              <div style="margin-top: 6px;" >
                                <p-switch v-model="switchVel.state" type="primary" on-text="ON" off-text="OFF" style="justify-content:center"></p-switch>
                                <h5 class="card-title" style="margin-left: 25px;">Velocidad</h5>
                              </div>
                            </div>

                        </div>
                  </div>
                  <div class="span2">
                        <div class="col-sm-6 col-md-4 col-lg-3">
                            <div class="row">
                              <div >
                                <input type="search" class="form-control input-sm" placeholder="km / h" v-model="vmax">
                                <h5 class="card-title">Vel. Max</h5>
                              </div>
                            </div>

                            <div class="row">
                              <div>
                                <input type="search" class="form-control input-sm" placeholder="minutos" v-model="tol">
                                <h5 class="card-title">Tolerancia</h5>
                              </div>
                            </div>
                        </div>
                  </div>
                </div>
          `,
        data () {
          return {
            switchVel: {
              state: false
            },
            switchEvent: {
              state: false
            },
            switchTrip: {
              state: false
            },
            search: '',
            vmax: '',
            tol: ''
          }
        },
        components: {
            [Button.name]: Button,
            Card,
            PSwitch
        }
      })
      new Vue({
        el: '#modal',
        beforeCreate:  () => {
          swal({
            titleText: "Descarga de Reportes",
            showCancelButton: true,
            cancelButtonText: 'Cancelar',
            confirmButtonText: 'Descargar',
            // confirmButtonAriaLabel: 'glyphicon glyphicon-ok-sign',
            // cancelButtonAriaLabel: 'glyphicon glyphicon-remove-sign',
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            width: 800,
            html: '<div id="modal"><my-comp></my-comp></div>'
          })
        }
      })
    }

I hope it helps you

Greetings from Argentina

https://github.com/aledc7