我的 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 方法。
addPhoto方法用于调用ajax。 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)