简体   繁体   English

Vue.js:如何修复自定义组件中未显示的'B-modal'

[英]Vue.js: How to fix 'b-modal' not displaying within custom component

Tools: Vue.js Bootstrap-Vue ( https://bootstrap-vue.js.org/docs/components/modal ) 工具:Vue.js Bootstrap-Vue( https://bootstrap-vue.js.org/docs/components/modal

Problem: I am trying to display a popup each time a row is clicked within a bootstrap grid. 问题:每次在引导网格中单击一行时,我都试图显示一个弹出窗口。 Then once the modal is hidden it disappears and the selected item goes away 然后,一旦模态被隐藏,它就会消失并且所选项目消失

I created a custom component for the Modal. 我为Modal创建了一个自定义组件。 And I am now trying to programmatically get rid of the selected certificate. 现在,我正在尝试以编程方式摆脱所选的证书。

I have it working, but barely and it's very clunky. 我有它的工作,但勉强而且很笨拙。 I want a more seamless approach on how someone would tackle this problem who has more Vue experience than I do 我想要一种比我拥有更多Vue经验的人如何更无缝地解决这个问题的方法

/// Modal component ///模态组件

        <b-modal 
            size="lg"
            id="certificate-details-modal" 
            hide-footer
            header-bg-variant="dark"
            header-text-variant="light"
            @hidden="modalDismissed"
            v-model="expiringCertificate"
        >
            <template slot="modal-title">
                Certificate Details for <span class="certificateTitleHighlight">{{ expiringCertificate.commonName }}</span>
            </template>

            <b-container fluid>
                <b-row>
                    <b-col>
                        <b-badge pill variant="dark">Identified</b-badge>
                    </b-col>
                    <b-col class="text-center">
                        <b-badge pill variant="info">Ready</b-badge>
                    </b-col>
                    <b-col class="text-right">
                        <b-badge pill variant="success">Resolved</b-badge>
                    </b-col>
                </b-row>
   ...

/// Main Component ///主要组件

    <ExpiringCertificateDetail
        v-if="selectedCertificate"
        v-on:modalDismissed="resetSelectedCertificate"
        :expiringCertificate="selectedCertificate">
    </ExpiringCertificateDetail>
...
    data () {
       ...
       selectedCertificate = undefined
    },
    methods: {
        resetSelectedCertificate() {
            this.selectedCertificate = undefined;
        },
        rowSelected(certificate) {
            this.selectedCertificate = certificate[0];
            this.$bvmodal.show('certificate-details-modal')
        },

My goal would be to display a modal each time a row is clicked and have the selectedCertificate reset back to undefined once the modal is hidden (either closed or unfocused and closed (which should be the hidden event) 我的目标是每次单击一行时显示一个模式,并在模式被隐藏(关闭或未聚焦并关闭(应为隐藏事件)后,将selectedCertificate重置为未定义)

I have been thinking of two possible approaches. 我一直在想两种可能的方法。 Each of them use a separate component for the modal. 他们每个人都为模态使用一个单独的组件。

1. Use v-model for the current selected item 1.对当前所选项目使用v-model

Use the modal component as any other input: declare a v-model on the component. 将模态组件用作任何其他输入:在组件上声明一个v模型。 When the modal is hidden, reset the current item to null from inside the modal component. 隐藏模态后,从模态组件内部将当前项重置为null。 The v-model magic will do the rest. v型魔术将完成其余的工作。

Full example here: 完整示例如下:

https://codesandbox.io/s/bootstrap-vue-sandbox-w8j09 https://codesandbox.io/s/bootstrap-vue-sandbox-w8j09

2. Reset the current selected item from outside the modal component 2.从模态组件外部重置当前选定的项目

This is pretty much the approach you have shown in your code. 这几乎就是您在代码中显示的方法。 The modal component is only responsible for displaying the details. 模态组件仅负责显示详细信息。 When to show the modal, or when to set the current selected item is the parent's responsibility. 何时显示模态,或何时设置当前所选项目是父母的责任。

In this example, I used a similar implementation as yours. 在此示例中,我使用了与您类似的实现。 I just use v-model on the modal component to avoid the this.$bvmodal.show and make the code more 'declarative'. 我只是在模态组件上使用v-model来避免this.$bvmodal.show并使代码更具“声明性”。

https://codesandbox.io/s/bootstrap-vue-sandbox-rwll4 https://codesandbox.io/s/bootstrap-vue-sandbox-rwll4

Both approaches make them possible to change the details component into something other than a modal. 两种方法都可以将详细信息组件更改为模态之外的其他内容。 Although the first approach is less verbose, I would go for the second approach because is leaves the responsibility of showing/hiding the details from outside. 尽管第一种方法不太冗长,但我会选择第二种方法,因为这是从外部显示/隐藏细节的责任。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM