How do modal box components submit different events?

In the

project, many modal boxes are required to submit the form. The beginning and end of the modal box are all the same, and the middle content uses slot to insert different content. Click OK to submit the data to different interfaces.

shared modal box subcomponents:

<template>
  <div>
    <el-dialog
      :visible.sync="dialogVisible"
      :title="title"
      :width="width"
      :slot-key="slotKey"
      :show="show"
      @close="$emit("update:show", false)">
      <slot :name="slotKey" />
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false"> </el-button>
        <el-button size="small" type="primary" @click="submit"> </el-button>
      </span>
    </el-dialog>
  </div>

</template>

<script>
export default {
  methods: {
    submit() {
      this.$emit("submit")
      this.dialogVisible = false
    }
  }
}
</script>

parent component invokes modal box child component:

<base-dialog
    :show.sync="show"
    :width="width"
    :slot-key="slotKey"
    :title="title"
    @submit="submit">
    <template slot="account">
      <account-dialog :temp="temp"/>
    </template>
    <template slot="ban">
      <ban-dialog :temp="temp"/>
    </template>
</base-dialog>
    
<script>
export default {
  methods: {
    submit() {
      ....
    }
  }
}
</script>

I have done a similar function: only it encapsulates ElPopover , and many similar functions are similar. For example, when you click popover to confirm, call the corresponding API according to id and action . At that time, the data and calling functions were passed to the child components through props , and the business logic was of course handled in the parent component:

<template>
  <el-popover
    placement="top"
    :width="wrapWidth"
    v-model="visible">
    

{{ tip }}

<div class="btns"> <el-button @click="visible = false"></el-button> <el-button type="primary" @click="handleConfirm">{{ confirmText }}</el-button> </div> <span slot="reference"> <slot name="reference"></slot> </span> </el-popover> </template> ... props: { params: Object, uuid: String, action: String, confirm: Function } methods: { handleConfirm() { this.confirm(this.params) } } ... Usage: <my-popover tip="**" :params="{id: scope.row.id, action: '**'}" :confirm="handleConfrim"> <my-icon slot="reference" icon-class="cancel_isolation" title="" /> </my-popover> handleConfirm({ id, action }) { axios.post(url, data: {id, action})... }
The

form has fewer fields and can be encapsulated in this way if it has something in common. If the field gap is large, and the business function has a lot in common, it is not recommended to encapsulate it just for encapsulation. Just write forms one by one in ElDialog .

Menu