在 sweetalert2 内容中使用 vue 组件
Using vue component in sweetalert2 content
我有几个普通的Sweetalert2 modals in a Vue项目。我想在警报中使用自定义组件。例如:
<template>
<h1>Hello {{name}}</h1>
</template>
<script>
module.exorts: {
props: ["name"]
}
</script>
my_template.vue
而且,在我的 sweetalert 模式中:
swal({
titleText: "Hi",
html: '<my-template name="hello"></my-template>'
});
我不确定这是否可行或如何实现。
技术上看起来可行:
Vue.component('my-component', {
template: '<div>A custom component!</div>'
})
new Vue({
el: '#modal',
beforeCreate: swal({
titleText: "Hi",
html: '<div id="modal"><my-component></my-component></div>'
})
})
但您可能希望将其包装在一个函数中。看看我的fiddle:
这只是一个想法,对我来说它看起来不太好,但仍然有效。另外我必须提到,每次以这种方式打开对话框时,您都会创建新的 vue 实例。
选项 2 从评论到我的回答:
Vue.component('my-component', {
template: '<div>A custom component!</div>'
})
swal({
html: "<my-component></my-component>"
})
new Vue({
el: swal.getHtmlContainer()
})
我已经设法让它工作如下:
我在反引号之间包含了模板的所有逻辑:` `
您还需要编辑 vue.config.js 文件并在 configurewebpack 对象中添加:'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'
}
}
}
一旦完成,您必须重新启动项目“npm 运行 dev”
这是我的完整示例,经过测试并且可以正常工作
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>'
})
}
})
}
希望对你有帮助
此致
您可以在您的应用程序中呈现和隐藏内容:
<div id="swalHTML" style='display: none'>
<my-template name="hello"></my-template>
</div>
然后将元素的 innerHTML 传递给警报:
let el = document.getElementById('swalHTML')
swal({
html: el.innerHTML
});
我有几个普通的Sweetalert2 modals in a Vue项目。我想在警报中使用自定义组件。例如:
<template>
<h1>Hello {{name}}</h1>
</template>
<script>
module.exorts: {
props: ["name"]
}
</script>
my_template.vue
而且,在我的 sweetalert 模式中:
swal({
titleText: "Hi",
html: '<my-template name="hello"></my-template>'
});
我不确定这是否可行或如何实现。
技术上看起来可行:
Vue.component('my-component', {
template: '<div>A custom component!</div>'
})
new Vue({
el: '#modal',
beforeCreate: swal({
titleText: "Hi",
html: '<div id="modal"><my-component></my-component></div>'
})
})
但您可能希望将其包装在一个函数中。看看我的fiddle:
这只是一个想法,对我来说它看起来不太好,但仍然有效。另外我必须提到,每次以这种方式打开对话框时,您都会创建新的 vue 实例。
选项 2 从评论到我的回答:
Vue.component('my-component', {
template: '<div>A custom component!</div>'
})
swal({
html: "<my-component></my-component>"
})
new Vue({
el: swal.getHtmlContainer()
})
我已经设法让它工作如下:
我在反引号之间包含了模板的所有逻辑:` `
您还需要编辑 vue.config.js 文件并在 configurewebpack 对象中添加:'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'
}
}
}
一旦完成,您必须重新启动项目“npm 运行 dev”
这是我的完整示例,经过测试并且可以正常工作
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>'
})
}
})
}
希望对你有帮助
此致
您可以在您的应用程序中呈现和隐藏内容:
<div id="swalHTML" style='display: none'>
<my-template name="hello"></my-template>
</div>
然后将元素的 innerHTML 传递给警报:
let el = document.getElementById('swalHTML')
swal({
html: el.innerHTML
});