(Vue.js) 模态关闭事件

(Vue.js) modal close event

当我点击模态框的外围区域时,我想要和模态框的关闭按钮一样的事件。 (点击模态外区域时关闭模态的事件)

当前进度为点击关闭模态按钮时模态关闭

Carousel.vue

<template>
  <div>
    <div v-for="(item, index) in photos" :key="index">
      <div @click="imgClick(item)" style="cursor:pointer;">
        <img :src="item.thumbnail" />
      </div>
      <Modal v-if='item.show' @close="item.show = false">
        <div slot='body'>
          <img :src="item.thumbnail" :class="`img-index--${index}`"/>
        </div>        
      </Modal>
    </div>
  </div>
</template>
<script>
import Modal from './Modal.vue'
export default {
  props: {
    items: { type: Array, default: () => [] }
  },
  data() {
    return {
      photos: {}
    }
  },
  created() {
    this.photos = this.items.map(item => {
      return { ...item, show: false }
    })
  },
  methods: {
    imgClick(item) {
      item.show = true
    }
  },
  components: {
    Modal: Modal
  }
}
</script>

Modal.vue

<template>
  <transition name="modal">
    <div class="modal-mask" @click="$emit('close')">
      <div class="modal-wrapper">
        <div class="app__phone">
          <div class="feed">
            <div class="post">
              <div class="header headroom">
                <div class="level-left">
                  <img src="../assets/imgs/user.gif" class="modal-header-img"/>
                  <div class="user">
                    <span class="username">username</span>
                    <button class="modal-default-button" @click="$emit('close')">Close</button>
                  </div>
                </div>
              </div>
              <slot name="modal-img"></slot>
              <div class="content">
                <div class="content-title">
                  <slot name="modal-tit"></slot>
                </div>
              </div>
            </div>
          </div> 
        </div>
      </div>
    </div>
  </transition>
</template>

当我在底部添加点击事件时<div>,当我点击模态框外时它会关闭,但当我点击模态框内的任何地方时它会关闭。

<div class="modal-mask" @click="$emit('close')">

并且这个 link 在接受的问题答案中有一个 Fiddle 示例。

您需要像这样向外部模态 div 添加处理程序:

<template id="modal">
  <div class="modal" @click.self="close">
    <div class="close" @click="close">&times;</div>
    <div class="body">
      <slot name="body" />
    </div>
  </div>
</template>