element plus +vue3 处理子组件Dialog样式无法更改的问题

正常页面加上deep深度查询是可以访问到的,但是子组件确实访问不到

父组件代码

<script setup>
import TheWelcome from '../components/TheWelcome.vue'
</script>

<template>
  <main>
    <TheWelcome />
  </main>
</template>

子组件代码

<template>
  <el-button text @click="centerDialogVisible = true">
    打开子组件弹框
  </el-button>
  <el-dialog
    v-model="centerDialogVisible"
    title="子组件弹框"
    width="30%"
    center
  >
    <span> 我是打开的子组件弹框 </span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="centerDialogVisible = false">
          确认
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script  setup>
import { ref } from "vue";

const centerDialogVisible = ref(false);
</script>
<style scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}

:deep(.el-dialog) {
  border: red 5px solid;
}
</style>


效果图

在这里插入图片描述
样式并没有改变,所以这样写深度查询子组件是无法查到的

解决办法:添加一个父类class,因为在子组件上可以精准的找到父类class,通过父类class找到弹框的样式,就可以进行更改了。

子组件改变后的代码

<template>
  <el-button text @click="centerDialogVisible = true">
    打开子组件弹框
  </el-button>
  <div class="dialogClass">
    <el-dialog
      v-model="centerDialogVisible"
      title="子组件弹框"
      width="30%"
      center
    >
      <span> 我是打开的子组件弹框 </span>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="centerDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="centerDialogVisible = false">
            确认
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script  setup>
import { ref } from "vue";

const centerDialogVisible = ref(false);
</script>
<style scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}

.dialogClass :deep(.el-dialog) {
  border: red 5px solid;
}
</style>

改变后的效果图

在这里插入图片描述

  • 16
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面为您提供一份实现el-dialog弹框拖拽的代码,包含详细注释,并且可以作为公共方法供其他组件调用。 首先,我们先创建一个mixin,命名为`dialogDragMixin.ts`,在该文件中实现弹框拖拽的功能。 ```typescript import { DirectiveBinding } from 'vue'; import { ElDialog } from 'element-plus'; // 定义接口,用于存储弹框的位置信息 interface DialogPosition { top: number; left: number; } // 定义一个变量,用于存储弹框的位置信息 let dialogPosition: DialogPosition = { top: 0, left: 0 }; export default { // 指令钩函数,当元素插入到 DOM 中时执行 mounted(el: HTMLElement, binding: DirectiveBinding) { // 获取el-dialog组件实例对象 const dialog: ElDialog = binding.instance.$parent as ElDialog; // 获取弹框的标题栏元素 const dialogHeaderEl: HTMLElement = el.querySelector('.el-dialog__header')!; // 设置标题栏的样式,使其可以被拖拽 dialogHeaderEl.style.cursor = 'move'; dialogHeaderEl.style.userSelect = 'none'; // 鼠标按下事件处理函数 const handleMouseDown = (event: MouseEvent) => { // 记录鼠标按下时的位置信息 dialogPosition.top = dialog.$el.offsetTop; dialogPosition.left = dialog.$el.offsetLeft; // 记录鼠标按下时的坐标信息 const mouseX = event.clientX; const mouseY = event.clientY; // 鼠标移动事件处理函数 const handleMouseMove = (event: MouseEvent) => { // 计算鼠标移动时的偏移量 const offsetX = event.clientX - mouseX; const offsetY = event.clientY - mouseY; // 更新弹框的位置信息 dialog.$el.style.top = dialogPosition.top + offsetY + 'px'; dialog.$el.style.left = dialogPosition.left + offsetX + 'px'; }; // 鼠标抬起事件处理函数 const handleMouseUp = () => { // 移除鼠标移动事件处理函数和鼠标抬起事件处理函数 document.removeEventListener('mousemove', handleMouseMove); document.removeEventListener('mouseup', handleMouseUp); }; // 添加鼠标移动事件处理函数和鼠标抬起事件处理函数 document.addEventListener('mousemove', handleMouseMove); document.addEventListener('mouseup', handleMouseUp); }; // 添加鼠标按下事件处理函数 dialogHeaderEl.addEventListener('mousedown', handleMouseDown); } } ``` 接下来,我们在需要使用该mixin的组件中引入它,并将其添加到`mixins`属性中即可。 ```vue <template> <el-dialog title="Dialog Title" :visible.sync="dialogVisible" width="30%" :before-close="handleClose" v-dialogDrag > <span>Dialog Content</span> <span slot="footer" class="dialog-footer"> <el-button @click="dialogVisible = false">Cancel</el-button> <el-button type="primary" @click="dialogVisible = false">Confirm</el-button> </span> </el-dialog> </template> <script lang="ts"> import { defineComponent } from 'vue'; import dialogDragMixin from './dialogDragMixin'; export default defineComponent({ name: 'MyComponent', mixins: [dialogDragMixin], data() { return { dialogVisible: false }; }, methods: { handleClose(done: () => void) { this.$confirm('Confirm to close this dialog?') .then(() => { done(); }) .catch(() => {}); } } }); </script> ``` 最后,我们可以将`dialogDragMixin`作为一个公共方法,供其他多个组件调用。在`main.ts`中,全局注册该mixin即可。 ```typescript import { createApp } from 'vue'; import App from './App.vue'; import ElementPlus from 'element-plus'; import 'element-plus/lib/theme-chalk/index.css'; import dialogDragMixin from './dialogDragMixin'; const app = createApp(App); // 注册全局mixin app.mixin(dialogDragMixin); app.use(ElementPlus).mount('#app'); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值