【问题标题】:How can I display modal after ajax response success on vue?如何在 vue 上的 ajax 响应成功后显示模态?
【发布时间】:2018-09-11 01:12:00
【问题描述】:

我的 vue 组件是这样的:

<template>
    ...
    <b-modal id="modalInvoice" size="lg"  title="Invoice">
        <Invoice/>
        <div slot="modal-footer" class="w-100"> 
            <b-btn size="sm" class="float-right" variant="warning" @click="show=false">
                <i class="fa fa-print"></i> Print
            </b-btn>
        </div>
    </b-modal>

    ...
        <b-btn variant="warning" class="btn-square mt-2" v-b-modal.modalInvoice @click="checkout()"><i class="fa fa-credit-card-alt"></i>Checkout</b-btn>
    ...
</template>
<script>
    ...
    export default {
        ...
        methods: {
            checkout() {
                var request = new Request(ApiUrl.orders, {
                    method: 'post',
                    body: JSON.stringify(this.$session.get(SessionKey.Cart)),
                    headers: new Headers({
                        'Authorization': 'bearer ' + this.$session.get(SessionKeys.ApiToken),
                        'Content-Type': 'application/json'
                    })
                });
                fetch(request).then(r=> r.json())
                    .then(r=> {
                        this.items = r.data
                    })
                    .catch(e=>console.log(e));
            }
        }
    }
</script>

如果按钮checkout被点击,modal会先显示不等待响应ajax成功

我想在响应ajax成功后做出模态显示

我该怎么做?

【问题讨论】:

    标签: javascript vue.js vuejs2 bootstrap-modal vue-component


    【解决方案1】:

    使用v-model 控制模态可见性:

    <b-modal id="modalInvoice" size="lg"  title="Invoice" v-model="show">
        <Invoice/>
        <div slot="modal-footer" class="w-100"> 
            <b-btn size="sm" class="float-right" variant="warning" @click="show=false">
                <i class="fa fa-print"></i> Print
            </b-btn>
        </div>
    </b-modal>
    

    这样你可以在 fetch 返回时显式设置show 属性:

    <script>
        ...
        export default {
            ...
            // a data property for the v-model to bind
            data () {
                return {
                     show: false
                }
            }
            methods: {
                checkout() {
                    var request = new Request(ApiUrl.orders, {
                        method: 'post',
                        body: JSON.stringify(this.$session.get(SessionKey.Cart)),
                        headers: new Headers({
                            'Authorization': 'bearer ' + this.$session.get(SessionKeys.ApiToken),
                            'Content-Type': 'application/json'
                        })
                    });
                    fetch(request).then(r=> r.json())
                        .then(r=> {
                            this.items = r.data
                            // toggle the value of show to display the modal
                            this.show = true
                        })
                        .catch(e=>console.log(e));
                }
            }
        }
    </script>
    

    【讨论】:

    • 如果使用:this.show = !this.show,当modal打开时,modal再次关闭。我尝试更改为this.show = true 并解决了
    • 我再次需要你的帮助。看看这个:stackoverflow.com/questions/52285601/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    • 2021-11-30
    相关资源
    最近更新 更多