I have a few plain Sweetalert2 modals in a Vue project. I want to use a custom ponent inside an alert. For example:
<template>
<h1>Hello {{name}}</h1>
</template>
<script>
module.exorts: {
props: ["name"]
}
</script>
my_template.vue
And, in my sweetalert modal:
swal({
titleText: "Hi",
html: '<my-template name="hello"></my-template>'
});
I'm not sure if this is even possible or how to do it.
I have a few plain Sweetalert2 modals in a Vue project. I want to use a custom ponent inside an alert. For example:
<template>
<h1>Hello {{name}}</h1>
</template>
<script>
module.exorts: {
props: ["name"]
}
</script>
my_template.vue
And, in my sweetalert modal:
swal({
titleText: "Hi",
html: '<my-template name="hello"></my-template>'
});
I'm not sure if this is even possible or how to do it.
Share Improve this question asked Feb 8, 2018 at 10:35 angrykoalaangrykoala 4,0648 gold badges35 silver badges59 bronze badges3 Answers
Reset to default 7Technically it looks possible:
Vue.ponent('my-ponent', {
template: '<div>A custom ponent!</div>'
})
new Vue({
el: '#modal',
beforeCreate: swal({
titleText: "Hi",
html: '<div id="modal"><my-ponent></my-ponent></div>'
})
})
But you may want to wrap it in a function. Take a look at my fiddle:
JS Fiddle
It's just an idea, for me it doesn't look good, but still working. Also I have to mention, that you will create new instance of vue every time you open your dialog this way.
Option 2 from ment to my answer:
Vue.ponent('my-ponent', {
template: '<div>A custom ponent!</div>'
})
swal({
html: "<my-ponent></my-ponent>"
})
new Vue({
el: swal.getHtmlContainer()
})
Fiddle
You can render and hide the content inside your app:
<div id="swalHTML" style='display: none'>
<my-template name="hello"></my-template>
</div>
Then pass the element's innerHTML to the alert:
let el = document.getElementById('swalHTML')
swal({
html: el.innerHTML
});
I have managed to make it work as follows:
I include all the logic of the template between backticks: ` `
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 ponents inside swal alert
'vue$':'vue/dist/vue.esm.js'
}
}
}
Once this is done you must relaunch the project "npm run dev"
This is my plete example, tested and working
openSweet() {
Vue.ponent('my-p', {
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: ''
}
},
ponents: {
[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-p></my-p></div>'
})
}
})
}
I hope it helps you
Regards