在vue组件中单击“保存”按钮后,如何关闭模态?

成功男人

我的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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

单击按钮后关闭模态

单击后禁用按钮或立即关闭模态

如何通过单击包含在另一个 vue 组件中的按钮来获取 vue 组件的内容(模态窗口)?

在引导程序中关闭模态后,如何取消模态触发按钮的焦点

单击按钮后,模态主体不会在 html 中呈现组件

在Vue.Js中单击“确定”后,关闭模态对话框

单击模态确定按钮时如何从另一个组件提交表单(bootstrap vue)

单击按钮后如何关闭弹出模式

单击模态内的按钮时如何在Angular 7中隐藏组件?

单击javafx中的按钮后如何保存/存储变量

关闭模态后,如何从模态主体中删除内容?

单击按钮后,React中的组件重复

关闭后如何在vue中重置模态中的数据值

vue单击父按钮,使子组件中的按钮单击

如何知道在按下模态上的关闭按钮后哪个按钮触发了模态

单击“保存”按钮后如何更新JTextfield

在 Javascript 中从模态弹出窗口编辑单击保存按钮后,列表项 <li> 的文本没有正确反映

在Joomla 2.5.16中单击“保存/保存并关闭”后如何修复空白页

如何通过Selenium和C#根据HTML在模态对话框中单击文本为“关闭”的按钮

模态脚本:模态中的关闭按钮

单击“保存”按钮,关闭Dropzone

在 React 中单击另一个组件中的按钮后如何显示 Modal?

关闭后如何清除模态中的反应状态?

如何基于模态中单击哪个按钮来发送参数?

单击 React.js 中的引导模式按钮后如何重新渲染组件

PyQt5:如何使按钮在单击后关闭 gui

react Portal-在模态外部单击时如何关闭模态,以及如何在父组件渲染模态内部将ref分配给模态?

Vue JS:使用按钮打开菜单组件,单击菜单外部单击关闭

单击odoo 12中的按钮后关闭向导