vue.js

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript类库 > vue.js > 前端嵌套多个el-dialog

前端在el-dialog中嵌套多个el-dialog代码实现

作者:Vamp_Piece

最近使用vue+elementUI做项目,使用过程中很多地方会用到dialog这个组件,有好几个地方用到了dialog的嵌套,下面这篇文章主要给大家介绍了关于前端在el-dialog中嵌套多个el-dialog代码实现的相关资料,需要的朋友可以参考下

一、应用场景

二、代码实现

<template>
  <table>
    <template #action>
      <el-button type="success" @click="outerVisible = true">
        修改备注
      </el-button>
    </template>
  </table>

  <el-dialog v-model="outerVisible" title="提示" draggable width="520px">
    <!-- #default:自定义内容均可写在此处 -->
    <template #default>
      <!-- 这里的el-form是外层dialog中的表单 -->
      <el-form label-width="100px" :model="note">
        <el-form-item label="备注" prop="note">
          <el-input v-model="note" />
        </el-form-item>
      </el-form>
      <!-- 内嵌的dialog -->
      <el-dialog
        v-model="innerVisible"
        width="30%"
        title="提示"
        append-to-body
        draggable
      >
        <span>请确认是否发送数据?</span>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="closeAllDialog">关闭</el-button>
            <el-button type="primary" @click="saveConfirm"> 确认 </el-button>
          </div>
        </template>
      </el-dialog>
    </template>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="outerVisible = false">{{
          t('global.action.close')
        }}</el-button>
        <el-button type="primary" @click="innerVisible = true">
          {{ t('global.action.confirm') }}
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, reactive } from 'vue';
import { Data, sendData } from '@/services/listData';

interface ViewState {
  selectedOrders: Data[];
  note: string;
  outerVisible: boolean;
  innerVisible: boolean;
}

export default defineComponent({
  name: 'list',
  components: {},
  setup() {
    const state = reactive<ViewState>({
      note: '',
      outerVisible: false,
      innerVisible: false,
    });

    // 关闭内层对话框的同时也关闭外层的对话框
    function closeAllDialog() {
      // 关闭内层的对话框
      state.innerVisible = false;
      // 关闭外层的对话框
      state.outerVisible = false;
    }

    function saveConfirm() {
      // 所勾选的id
      const selectedIds = selection.value.map(i => {
        return i.id;
      });
      // 这里写请求接口的逻辑
      sendData(selectedIds, state.note)
        .then(() => {
          ElMessage({
            type: 'success',
            message: '发送成功',
          });
        })
        .finally(() => {
          state.innerVisible = false;
          state.outerVisible = false;
        });
    }

    return {
      ...toRefs(state),
      saveConfirm,
      closeAllDialog,
    };
  },
});
</script>

<style scoped lang="scss"></style>

附:el-dialog嵌套el-dialog问题

⼀定要⽗级 el-dialog :modal-append-to-body=“false”

⼦级 el-dialog 同时加上 :append-to-body=“true”

含义:

总结 

到此这篇关于前端在el-dialog中嵌套多个el-dialog的文章就介绍到这了,更多相关前端嵌套多个el-dialog内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文