【问题标题】:Append bootstrap-vue modal to app template将 bootstrap-vue 模式附加到应用程序模板
【发布时间】:2019-09-21 12:33:35
【问题描述】:

我用bootstrap-vue modal:

在我的项目codesandbox

模板:

<template>
  <b-button variant="link" class="btn-remove" @click="removeItemFromOrder(index)">
    Remove item
  </b-button>
</template>

使用自定义内容生成模式的脚本:

<script>
export default {
  name: "HelloWorld",
  methods: {
    removeItemFromOrder: async function (position) {
        let self = this

        const h = this.$createElement

        const titleVNode = h('div', { domProps: { innerHTML: '<h2>Remove this item?</h2>' } })

        const messageVNode = h('div', { class: ['modal-complete'] }, [

          h('div', {
            domProps: {
              innerHTML:  '<h2>Remove this item?</h2>'+
                          '<span class="popup-meta">'+
                            'Are you sure you want to remove this item?'+
                          '</span>'
            }
          })
        ])


        this.$bvModal.msgBoxConfirm([messageVNode], {
          title: [],
          centered: true,
          modalClass: 'success-popup',
          hideHeader:true,
          footerClass: 'd-flex justify-content-center align-items-center',
          cancelVariant: 'outline-danger',
          okVariant: 'danger',
          okTitle: 'YES',
          cancelTitle: 'NO',
          ststic: false
        })
          .then(async function (result) {
            if (result) {
              self.currentOrder.items.splice(position, 1)
              await self.sync()
            }
          })
          .catch(err => {
            // An error occurred
          })
      },
  }
};
</script>

现在打开追加到body 后的引导模式。所以,这就是为什么我有一个问题

如何将 bootstrap-vue 模态附加到 #app 或其他模板\标签?

P.S: 仅适用于this.$bvModal.msgBoxConfirmthen ...以免创建不必要的方法... 由于具有不同逻辑的弹出窗口数量

【问题讨论】:

  • 你到底想做什么?
  • 以免创建不必要的方法
  • 由于逻辑不同的弹出窗口数量
  • 我已经看到了这个问题,但我希望仍然有解决这个问题的方法!无需其他方法即可创建多个模态的任何其他解决方案或模态插件

标签: javascript vue.js


【解决方案1】:

这是不可能的 如果您阅读代码,它只是将 Modal 直接附加到正文

https://github.com/bootstrap-vue/bootstrap-vue/blob/dev/src/components/modal/helpers/bv-modal.js#L162

  const div = document.createElement('div')
  document.body.appendChild(div)
  msgBox.$mount(div)

【讨论】:

    【解决方案2】:

    我遇到了同样的问题并想出了一个解决方法。我正在使用引导程序的范围版本,其中每个引导程序类都以.bootstrap-scope 为前缀,以便引导程序样式不会干扰页面上的其他内容。这意味着将模态直接附加到正文中是行不通的,因为它不会从 .bootstrap-scope 中继承引导样式(而且我无法将 bootstrap-scope 类添加到正文中)。

    我已经加载了 jquery,所以我的解决方案是简单地捕获 $bvModal.show() 事件,方法是立即隐藏新创建并附加到正文(不会正确显示)的模式,然后附加模态到我想要的容器。需要注意的是,模态框在隐藏时需要从自定义容器中移除,因为 show 事件会重新创建它并且会出现问题。

    show_modal: function(viz) {
        var vm = this;              
    
        vm.$bvModal.show(vm.modalId); 
        $(`#${vm.modalId}___BV_modal_outer_`).hide(); 
    
        setTimeout(()=>{
            $(`#${vm.modalId}`).appendTo('#' + vm.containerId); 
            $(`#${vm.modalId}___BV_modal_outer_`).show(200); 
        }, 50); 
    
    }
    
    
    
    hide_modal: function() {
        this.$bvModal.hide(this.modalId}); 
        $(`#${this.containerId} .modal`).remove(); 
    }
    

    【讨论】:

      猜你喜欢
      • 2013-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-28
      • 2017-12-29
      • 2012-09-01
      • 1970-01-01
      • 2018-05-10
      相关资源
      最近更新 更多