每当点击标题上的任何按钮时,我都会try 显示确认组件.每当我单击其他元素时,它都会打开,除非使用v菜单从下拉菜单中 Select 其他元素.

应用视图

<template>
  {{isConfirmDialogVisible}}
  <div class="header">
    <div class="left-menus">
      <div
        v-for="(item, index) in items"
        :key="index"
        class="menu"
        style="background-color: red; gap: 10px; padding: 10px"
        @click="opendialog()"
      >
        {{ item.title }}
      </div>
    </div>
    <v-menu open-on-hover transition="slide-y-transition">
      <template #activator="{ props }">
        <div style="cursor: pointer">
          <v-icon v-bind="props">mdi-menu-down</v-icon>
        </div>
      </template>

      <v-list>
        <v-list-item
          v-for="(item, index) in userOptions"
          :key="index"
          class="dropdown"
          @click="opendialog()"
        >
          <v-list-item-title>{{ item.title }}</v-list-item-title>
        </v-list-item>
      </v-list>
    </v-menu>
    <Confirmation
      v-model="isConfirmDialogVisible"
      v-if="isConfirmDialogVisible"
      action-button="submit"
      action-button-left="cancle"
      action-button-right="confirm"
    >
      <div style="font-size: 20px">Are you sure?</div>
    </Confirmation>
  </div>
</template>

<script>
  import { ref } from 'vue'
  import Confirmation from './Confirmation.view'

  const isConfirmDialogVisible = ref(false)

  const userOptions = ref([{ title: 'Logout' }, { title: 'Setting' }])

  const opendialog = () => {
    isConfirmDialogVisible.value = true
  }

  export default {
    components: {
      Confirmation,
    },
    data: () => ({
      items: [
        { title: 'Click Me' },
        { title: 'Click Me' },
        { title: 'Click Me' },
        { title: 'Click Me 2' },
      ],

      opendialog,
      isConfirmDialogVisible,
      userOptions,
    }),
  }
</script>

<style>
  .header .left-menus {
    display: flex;
    align-items: center;
    gap: 16px;
  }
</style>

Confirmation.view

<template>
  <v-dialog v-model="dialogVisible" persistent activator="parent" width="auto">
    <div class="dialog_container">
      <v-card>
        <div class="card-components">
          <div class="warning_image"></div>
          <v-card-text>
            <slot></slot>
          </v-card-text>
          <v-card-actions>
            <v-btn class="back" @click="closeDialog"
              >{{ actionButtonLeft }}</v-btn
            >
            <v-btn @click="confirmSelection" class="confirm-submit">
              {{ actionButtonRight}}
            </v-btn>
          </v-card-actions>
        </div>
      </v-card>
    </div>
  </v-dialog>
</template>
<script>
  import { ref } from 'vue'

  export default {
    props: {
      modelValue: Boolean,
      actionButton: {
        type: String,
        required: true,
      },
      actionButtonRight: {
        type: String,
        required: true,
      },
      actionButtonLeft: {
        type: String,
        required: false,
        default: 'cancel',
      },
    },

    emits: ['update:modelValue', 'confirmAction'],
    setup(props, { emit }) {
      const dialogVisible = ref(false)
      const closeDialog = () => {
        dialogVisible.value = false
        emit('update:modelValue', false)
      }
      const confirmSelection = () => {
        closeDialog()
        emit('confirmAction')
      }

      const returnBack = () => {
        closeDialog()
      }

      const handleOutsideClick = event => {
        console.log(event)
      }

      return {
        dialogVisible,
        closeDialog,
        returnBack,
        confirmSelection,
        handleOutsideClick,
      }
    },
  }
</script>

<style scoped>
  .card-components {
    display: flex;
    flex-direction: column;
    gap: 24px;
  }

  .dialog_container {
    height: 225px;
    width: 648px;
  }
  .v-card {
    padding: 16px 24px 24px 24px;
  }
  .v-card-text {
    padding: 0;
  }
  .warning_image {
    display: flex;
    width: 100%;
    justify-content: center;
  }

  .v-card-actions {
    min-height: 0;
    padding: 0;
    display: flex;
    justify-content: flex-end;
  }
  .v-card-actions .v-btn {
    border: 1px solid #9ca3af;
    color: #9ca3af;
    width: 140px;
    justify-content: center;
    align-items: center;
    border-radius: 4px;
  }

  .v-card-actions .confirm-submit {
    color: white;
    border: 1px solid #41a1e0;
    background: #41a1e0;
  }
</style>

每当点击标题上的任何按钮时,我都会try 显示确认组件.每当我单击其他元素时,它都会打开,除非使用v菜单从下拉菜单中 Select 其他元素.

推荐答案

在Conformation.vue中,VDialog依赖于父组件变为可见:

<template>
  <v-dialog v-model="dialogVisible" activator="parent">
  ...
</template>
<script setup>
  const dialogVisible = ref(false)
  ...
</script>

因此,当确认组件变为可见时,VDialog将显示该对话框并更新dialogVisible.

由于某些原因,当VMenu可见时,这不起作用.相反,使用组件的modelValue来触发该对话框,这无论如何都更有意义.有几种方法可以做到这一点,一种是在观察器中设置dialogVisible:

setup(props, { emit }) {
  const dialogVisible = ref(false)
  watchEffect(() => dialogVisible.value = props.modelValue)

然后,你必须go 掉activator="parent",它才能工作.

这是一张playground元的

Javascript相关问答推荐

在JavaScript中检索一些文本

如何编辑代码FlipDown.js倒计时?

JS生成具有给定数字和幻灯片计数的数组子集

如何判断属于多个元素的属性是否具有多个值之一

使用JavaScript重新排序行

如何为我的astro页面中的相同组件自动创建不同的内容?

Angular 中的类型错误上不存在获取属性

如何从HTML对话框中检索单选项组的值?

在forEach循环中获取目标而不是父对象的属性

Webpack在导入前混淆文件名

如何在Bootstrap中减少网格系统中单个div的宽度

从另一个数组中的对应行/键值对更新数组中的键值对对象

在Odoo中如何以编程方式在POS中添加产品

如何在独立的Angular 应用程序中添加Lucide-Angel?

如何正确地在ComponentWillUnmount中卸载状态以避免内存泄漏?

如何检测当前是否没有按下键盘上的键?

找不到处于状态的联系人

如何在不将整个文件加载到内存的情况下,在Node.js中实现Unix粘贴命令?

Rails 7:在不使用导入映射的情况下导入Java脚本

递归地将JSON对象的内容上移一级