我的Vue组件是这样的:
<template>
<div ref="modal" class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form>
...
<div class="modal-footer">
...
<button type="button" class="btn btn-success" @click="addPhoto">
Save
</button>
</div>
</form>
</div>
</div>
</div>
</template>
<script>
export default {
...
methods: {
addPhoto() {
const data = { id_product: this.idProduct };
this.$store.dispatch('addImageProduct', data)
.then((response) => {
this.$parent.$options.methods.createImage(response)
});
},
}
}
</script>
如果单击按钮addPhoto,它将调用addPhoto方法。
用于调用ajax的addPhoto方法。ajax响应后,它将响应传递给父组件中的createImage方法
运行后,模态不会关闭。单击保存按钮后,模态是否应该关闭
调用createImage方法后如何关闭模态?
您不能以此方式同时使用Bootstrap和Vue。每个人都想控制DOM,他们将踩在彼此的脚趾上。要一起使用它们,您需要包装器组件,以便Bootstrap可以控制组件内部的DOM,而Vue可以与包装器通信。
幸运的是,有一个项目已经为Bootstrap小部件制作了包装器组件。这是Bootstrap-Vue。当您按下确定按钮时,其模式将自动关闭。我已经把他们的模态例子变成了模糊的样子,就像您要尝试的那样。
new Vue({
el: '#app',
methods: {
clearName() {
this.name = '';
},
addPhoto(e) {
console.log("dispatch the request");
}
}
});
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap@next/dist/css/bootstrap.min.css" />
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.css" />
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js"></script>
<script src="//unpkg.com/babel-polyfill@latest/dist/polyfill.min.js"></script>
<script src="//unpkg.com/tether@latest/dist/js/tether.min.js"></script>
<script src="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.js"></script>
<div id="app">
<div>
<b-btn v-b-modal.modal1>Launch demo modal</b-btn>
<!-- Modal Component -->
<b-modal id="modal1" title="Whatever" ok-title="Save" @ok="addPhoto" @shown="clearName">
<form @submit.stop.prevent="submit">
Form stuff...
</form>
</b-modal>
</div>
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句