如何使用Vue绑定Boostrap模式数据

时间:2018-06-23 20:52:01

标签: javascript html5 vuejs2 bootstrap-4 vue-component

要显示引导程序模态,我们可以使用data-toggle属性。例如: <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#msg">Open Modal</button>

我想通过使用vue bool变量数据来显示模态。这是我的组件:

Vue.component('modal', {
delimiters: ['[[', ']]'],
props: {
    id: String,
    hidden: Boolean
},
data: function () {
    return {
    }
},
computed: {
    getStyle: function() {
        if(this.hidden)
            return "display: hidden;"
        return "display: block;"
    }
},
methods: {
},
template: `
    <div class="modal fade" v-bind:class="{show: !hidden}" role="dialog" :style="getStyle" :id="id">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <slot name="header"></slot>
                </div>
                <div class="modal-body">
                    <slot></slot>
                </div>
            </div>
        </div>
    </div>
`
})

比html中的我有:

<div id="msg_modal">
  <modal :id="'msg'" :hidden='isHidden'>
    {{ message }}
  </modal>
</div>

和脚本中

<script>
  var msg_modal = new Vue({
    el: '#msg_modal',
    data: {
      isHidden: false
    }
  })  
</script>

此后,我的模态是可见的,但是行为与单击“打开模态”按钮不同。例如,我不能通过单击模态边界来关闭模态。

预先感谢您的帮助。


编辑: 我通过使用jquery临时修复了该问题

computed: {
    id_selector: function() {
        return "#"+this.id;
    }
},
methods: {
    show: function() {
        this.modal('show');
    },
    hide: function() {
        this.modal('hide');
    },
    toggle: function() {
        this.modal('toggle');

    },
    modal: function(action) {
        $(this.id_selector).modal(action);
    }
},

有人知道没有jquery的解决方案吗?

1 个答案:

答案 0 :(得分:0)

另一种方法是以编程方式打开/关闭模态(但您需要使用jquery):

Vue.component('modal', {
props: ['id'],
template: `
    <div class="modal fade" role="dialog" :id="id">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <slot name="header"></slot>
                </div>
                <div class="modal-body">
                    <slot></slot>
                </div>
            </div>
        </div>
    </div>
`
})
new Vue({
  el: '#msg_modal',
  data: {
    message: 'Modal content'
  },
  methods: {
    showModal() {
      let element = document.getElementById('msg')
      $(element).modal('show')
    }
  }
})  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>

<div id="msg_modal">
  <modal :id="'msg'">
    <template slot="header">Header</template>
    {{ message }}
  </modal>
  <button class="btn btn-info btn-lg" @click="showModal">Open Modal</button>
</div>