简体   繁体   English

VueJs:如何重用引导程序模式对话框

[英]VueJs: How to reuse bootstrap modal dialog

I've created three simple buttons that will trigger three different bootstrap modal dialog. 我创建了三个简单的按钮,它们将触发三个不同的引导模式对话框。 The modal dialogs are "Add Product" , "Edit Product" and "Delete Product" . 模态对话框为“添加产品”“编辑产品”“删除产品” Both the Add and Edit modal dialogs contain a form with two input elements, whereas the Delete modal dialog contains a simple text. 添加”和“ 编辑”模式对话框均包含具有两个输入元素的表单,而“ 删除”模式对话框则包含简单文本。 I realise that my code becomes very messy and hard to maintain. 我意识到我的代码变得非常凌乱并且难以维护。 Hence, I have the following question: 因此,我有以下问题:

1) How do I reuse the modal dialog, instead of creating 3 separate dialogs? 1)如何重用模式对话框,而不是创建3个单独的对话框?

2) How do I know which modal dialog has been triggered? 2)我怎么知道触发了哪个模态对话框?

Update : I've developed a soultion where I will include conditional statements such as v-if, v-else-if and v-else to keep track of which button the user click. 更新 :我已经开发了一个引人注意的地方,其中将包含条件语句,例如v-if,v-else-if和v-else,以跟踪用户单击哪个按钮。 However, I still feel that there is a better solution to this. 但是,我仍然觉得有一个更好的解决方案。 Can anyone help/advice me? 有人可以帮我吗?

Below is my current code: 下面是我当前的代码:

       <template>
  <div>
    <b-button v-b-modal.product class="px-4" variant="primary" @click="addCalled()">Add</b-button>
    <b-button v-b-modal.product class="px-4" variant="primary" @click="editCalled()">Edit</b-button>
    <b-button v-b-modal.product class="px-4" variant="primary" @click="deleteCalled()">Delete</b-button>

    <!-- Modal Dialog for Add Product -->
    <b-modal id="product" title="Add Product">
      <div v-if="addDialog">
        <form @submit.stop.prevent="submitAdd">
          <b-form-group id="nameValue" label-cols-sm="3" label="Name" label-for="input-horizontal">
            <b-form-input id="nameValue"></b-form-input>
          </b-form-group>
        </form>

        <b-form-group id="quantity" label-cols-sm="3" label="Quantity" label-for="input-horizontal">
          <b-form-input id="quantity"></b-form-input>
        </b-form-group>
      </div>

      <div v-else-if="editDialog">
        <form @submit.stop.prevent="submitEdit">
          <b-form-group id="nameValue" label-cols-sm="3" label="Name" label-for="input-horizontal">
            <b-form-input id="nameValue" :value="productName"></b-form-input>
          </b-form-group>
        </form>

        <b-form-group id="quantity" label-cols-sm="3" label="Quantity" label-for="input-horizontal">
          <b-form-input id="quantity" :value="productQuantity">5</b-form-input>
        </b-form-group>
      </div>

      <div v-else>
        <p class="my-4">Are You Sure you want to delete product?</p>
      </div>
    </b-modal>
  </div>
</template>

<script>
export default {
  data() {
    return {
      productName: "T-Shirt",
      productQuantity: 10,
      addDialog: false,
      editDialog: false,
      deleteDialog: false
    };
  },
  methods: {
    addCalled() {
      this.addDialog = true;
    },
    editCalled() {
      this.editDialog = true;
      this.addDialog = false;
      this.deleteDialog = false;
    },
    deleteCalled() {
      this.deleteDialog = true;
      this.addDialog = false;
      this.editDialog = false;
    }
  }
};
</script>

<style>
</style>

Update 更新资料

Now, You might think how will you close your modal and let the parent component know about it. 现在,您可能会想如何关闭模态并让父组件知道它。 On click of button trigger closeModal for that 在单击按钮时触发closeModal

Create a method - closeModal and inside commonModal component and emit an event. 创建一个方法closeModal并在commonModal组件内部并emit一个事件。

closeModal() { 
  this.$emit('close-modal')
}

Now this will emit a custom event which can be listen by the consuming component. 现在,这将发出一个自定义事件,使用方组件可以侦听该事件。

So in you parent component just use this custom event like following and close your modal 因此,在父组件中,只需使用以下自定义事件即可,然后关闭模态

<main class="foo">
  <commonModal v-show="isVisible" :data="data" @close- modal="isVisible = false"/>
  <!-- Your further code -->
</main>

So as per your question 所以根据你的问题

A - How do I reuse the modal dialog, instead of creating 3 separate dialogs A- 如何重用模式对话框,而不是创建3个单独的对话框

Make a separate modal component, let say - commonModal.vue . 制作一个单独的modal组件,比如说commonModal.vue

Now in your commonModal.vue , accept single prop , let say data: {} . 现在,在您的commonModal.vue ,接受单个prop ,假设data: {}

Now in the html section of commonModal 现在在commonModalhtml部分

<div class="modal">
  <!-- Use your received data here which get received from parent -->
  <your modal code />
</div>

Now import the commonModal to the consuming/parent component. 现在将commonModal导入到使用/父组件。 Create data property in the parent component, let say - isVisible: false and a computed property for the data you want to show in modal let say modalContent . 在父组件中创建data属性,比如说isVisible: false和要在modal显示的data的计算属性,比如说modalContent

Now use it like this 现在这样使用

<main class="foo">
   <commonModal v-show="isVisible" :data="data" />
   <!-- Your further code -->
</main>

The above will help you re-use modal and you just need to send the data from parent component. 上面的内容将帮助您重用模式,而您只需要从父组件发送data

Now second question will also get solved here How do I know which modal dialog has been triggered? 现在,第二个问题也将在此处解决。 我如何知道已触发哪个模态对话框?

Just verify isVisible property to check if modal is open or not. 只需验证isVisible属性即可检查modal是否打开。 If isVisible = false then your modal is not visible and vice-versa 如果isVisible = false则您的模态不可见,反之亦然

As already mentionned, I would have use slots and dynamic component rendering to accomplish what you're trying to do in a cleaner way. 如前所述,我将使用插槽和动态组件渲染来以更清洁的方式完成您要尝试执行的操作。

See snippet below (I didn't make them modals as such but the idea is the same). 参见下面的代码片段(我没有将它们作为模态,但是想法是一样的)。

This way, you can have a generic modal component that deals with the shared logic or styles and as many modalContent sub-components as needed that are injected via the dedicated slot. 这样,您可以拥有一个处理通用逻辑或样式的通用modal组件,以及通过专用插槽注入的所需数量的modalContent子组件。

 Vue.component('modal', { template: ` <div> <h1>Shared elements between modals go here</h1> <slot name="content"/> </div> ` }); Vue.component('modalA', { template: ` <div> <h1>I am modal A</h1> </div> ` }); Vue.component('modalB', { template: ` <div> <h1>I am modal B</h1> </div> ` }); Vue.component('modalC', { template: ` <div> <h1>I am modal C</h1> </div> ` }); new Vue({ el: "#app", data: { modals: ['modalA', 'modalB', 'modalC'], activeModal: null, }, }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <button v-for="modal in modals" @click="activeModal = modal"> Open {{ modal }} </button> <modal> <template slot="content"> <component :is="activeModal"></component> </template> </modal> </div> 

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

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