javascript - 在 sweetalert2 内容中使用 vue 组件

标签 javascript vue.js sweetalert2

我有几个普通的Sweetalert2 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 :

JS Fiddle

这只是一个想法,对我来说它看起来不太好,但仍然有效。另外我必须提到,每次以这种方式打开对话框时,您都会创建新的 vue 实例。

选项 2 从评论到我的回答:

Vue.component('my-component', {
    template: '<div>A custom component!</div>'
})    

swal({
    html: "<my-component></my-component>"
})
  
new Vue({
    el: swal.getHtmlContainer()
})  
 

Fiddle

关于javascript - 在 sweetalert2 内容中使用 vue 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48683390/

相关文章:

flask - 由于不允许的预检 header ,Vue.js 前端与 Flask 后端交互出现 CORS 问题

javascript - VueJS - 从递归树中获取输入值

vue-component - Vuex。如何使用 v-for 从 store 渲染数据

html - 如何使用应用了 CSS 的 Sweetalert?

javascript - 未捕获( promise )使用 SweetAlert2 取消

javascript - 在 PHP 循环中显示/隐藏

javascript - 删除完整路径,仅保留文件名

JavaScript:仅从服务器生成的 Date().toTimeString() 中检索分钟

javascript - 如何让javascript sweetalert2的确认框返回true

javascript - 谷歌地图无法在 Chrome 上运行