element ui中父子组件共用一个el-dialog弹窗,切换组件页面弹窗进行关闭

在Element UI中,如果多个父子组件共用一个el-dialog弹窗,并且需要在切换组件页面时关闭弹窗,你可以考虑以下方法来实现:

使用Vuex进行状态管理:

在Vuex中创建一个状态来管理弹窗的显示状态(例如,showDialog)。 在父子组件中都可以访问这个状态,以便共享。

当需要打开或关闭弹窗时,分发对应的Vuex mutation 来更新showDialog状态。

在el-dialog中使用v-if或v-show根据showDialog的值来控制弹窗的显示与隐藏。

javascript 复制代码
// store.js
import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    showDialog: false,
  },
  mutations: {
    toggleDialog(state) {
      state.showDialog = !state.showDialog;
    },
  },
});

在父子组件中使用 mapState 和 mapMutations 来访问和修改 showDialog 状态:

html 复制代码
// ParentComponent.vue
<template>
  <div>
    <button @click="toggleDialog">Toggle Dialog</button>
    <child-component></child-component>
    <el-dialog :visible="showDialog" @close="toggleDialog">
      <!-- 弹窗内容 -->
    </el-dialog>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex';

export default {
  computed: {
    ...mapState(['showDialog']),
  },
  methods: {
    ...mapMutations(['toggleDialog']),
  },
};
</script>
html 复制代码
// ChildComponent.vue
<template>
  <div>
    <button @click="toggleDialog">Toggle Dialog</button>
  </div>
</template>

<script>
import { mapMutations } from 'vuex';

export default {
  methods: {
    ...mapMutations(['toggleDialog']),
  },
};
</script>

事件总线:

创建一个事件总线(Event Bus)作为Vue实例,使得不同组件可以通过该事件总线进行通信。

在需要打开或关闭弹窗的地方,触发事件,然后在el-dialog所在的组件中监听这些事件,以控制弹窗的显示与隐藏。

javascript 复制代码
// EventBus.js
import Vue from 'vue';
export default new Vue();

在需要打开或关闭弹窗的地方触发事件:

html 复制代码
// ParentComponent.vue
<template>
  <div>
    <button @click="toggleDialog">Toggle Dialog</button>
    <child-component></child-component>
    <el-dialog :visible="showDialog" @close="toggleDialog">
      <!-- 弹窗内容 -->
    </el-dialog>
  </div>
</template>

<script>
import EventBus from './EventBus';

export default {
  data() {
    return {
      showDialog: false,
    };
  },
  methods: {
    toggleDialog() {
      this.showDialog = !this.showDialog;
      EventBus.$emit('toggle-dialog', this.showDialog);
    },
  },
};
</script>
html 复制代码
// ChildComponent.vue
<template>
  <div>
    <button @click="toggleDialog">Toggle Dialog</button>
  </div>
</template>

<script>
import EventBus from './EventBus';

export default {
  methods: {
    toggleDialog() {
      EventBus.$emit('toggle-dialog', true);
    },
  },
};
</script>

在el-dialog所在的组件中监听事件:

html 复制代码
// AnyComponent.vue
<template>
  <el-dialog :visible="showDialog" @close="toggleDialog">
    <!-- 弹窗内容 -->
  </el-dialog>
</template>

<script>
import EventBus from './EventBus';

export default {
  data() {
    return {
      showDialog: false,
    };
  },
  created() {
    EventBus.$on('toggle-dialog', (showDialog) => {
      this.showDialog = showDialog;
    });
  },
};
</script>

使用provide和inject:

在父组件中使用provide来提供一个控制弹窗显示的方法,或者提供一个布尔值的ref。

在子组件中使用inject来获取这些提供的数据。

子组件可以调用提供的方法或者监听提供的ref来控制弹窗的显示与隐藏。

在父组件中使用provide来提供一个方法或ref:

html 复制代码
// ParentComponent.vue
<template>
  <div>
    <button @click="toggleDialog">Toggle Dialog</button>
    <child-component></child-component>
    <el-dialog :visible="showDialog" @close="toggleDialog">
      <!-- 弹窗内容 -->
    </el-dialog>
  </div>
</template>

<script>
export default {
  provide: {
    toggleDialog: this.toggleDialog, // 方法
    showDialogRef: this.showDialogRef, // ref
  },
  data() {
    return {
      showDialog: false,
      showDialogRef: ref(false),
    };
  },
  methods: {
    toggleDialog() {
      this.showDialog = !this.showDialog;
      this.showDialogRef.value = this.showDialog;
    },
  },
};
</script>

在子组件中使用inject来获取这些提供的数据:

html 复制代码
// ChildComponent.vue
<template>
  <div>
    <button @click="toggleDialog">Toggle Dialog</button>
  </div>
</template>

<script>
import { inject } from 'vue';

export default {
  setup() {
    const { toggleDialog, showDialogRef } = inject();
    const toggleDialogInParent = () => {
      toggleDialog();
    };

    return {
      toggleDialogInParent,
      showDialogRef,
    };
  },
};
</script>
相关推荐
D_C_tyu15 分钟前
Vue3 + Element Plus | el-table 表格获取排序后的数据
javascript·vue.js·elementui
JIngJaneIL16 分钟前
基于java+ vue农产投入线上管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
天外天-亮42 分钟前
v-if、v-show、display: none、visibility: hidden区别
前端·javascript·html
hellotutu1 小时前
vue2 从 sessionStorage 手动取 token 后,手动加入到 header
vue.js·token·session·header
沿着路走到底2 小时前
JS事件循环
java·前端·javascript
子春一22 小时前
Flutter 2025 可访问性(Accessibility)工程体系:从合规达标到包容设计,打造人人可用的数字产品
前端·javascript·flutter
jlspcsdn3 小时前
20251222项目练习
前端·javascript·html
一 乐4 小时前
酒店预约|基于springboot + vue酒店预约系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端
前端无涯4 小时前
React/Vue 代理配置全攻略:Vite 与 Webpack 实战指南
vue.js·react.js