5

我在页面上有几个嵌套组件,父组件具有@click.native实现。因此,当我单击子组件(位于父组件内)占用的区域时,例如执行两个单击操作(父组件和所有嵌套子组件)

<products>
   <product-details>
       <slide-show>
             <media-manager>
                  <modal-dialog>
   <product-details>
       <slide-show>
             <media-manager>
                  <modal-dialog>
 </products>

所以我有多个产品的列表,当我单击属于模态对话框的“画布”时 - 我也会被@click.native模态对话框所属的产品详细信息解雇。有类似的东西会很好@click.native.stop="code",这可能吗?

现在我必须这样做:

@click.native="clickHandler"
and then 

  methods: {
    clickHandler(e) {
      e.stopPropagation();
      console.log(e);
    }

代码

<template>
  <div class="media-manager">
    <div v-if="!getMedia">
      <h1>When you're ready please upload a new image</h1>
      <a href="#"
         class="btn btn--diagonal btn--orange"
         @click="upload=true">Upload Here</a>
    </div>
    <img :src="getMedia.media_url"
         @click="upload=true"
         v-if="getMedia">
    <br>
    <a class="arrow-btn"
       @click="upload=true"
       v-if="getMedia">Add more images</a>
    <!-- use the modal component, pass in the prop -->
    <ModalDialog
      v-if="upload"
      @click.native="clickHandler"
      @close="upload=false">
      <h3 slot="header">Upload Images</h3>
      <p slot="body">Hello World</p>
    </ModalDialog>
  </div>
</template>

<script>
import ModalDialog from '@/components/common/ModalDialog';
export default {
  components: {
    ModalDialog,
  },
  props: {
    files: {
      default: () => [],
      type: Array,
    },
  },
  data() {
    return {
     upload: false,
    }
  },
  computed: {
    /**
     * Obtain single image from the media array
     */
    getMedia() {
      const [
        media,
      ] = this.files;

      return media;
    },
  },
  methods: {
    clickHandler(e) {
      e.stopPropagation();
      console.log(e);
    }
  }
};
</script>

<style lang="scss" scoped>
.media-manager img {
  max-width: 100%;
  height: auto;
}

a {
  cursor: pointer;
}

</style>
4

3 回答 3

2

在 Vue 中,修饰符可以被链接。因此,您可以自由使用如下修饰符:

@click.native.prevent或者@click.stop.prevent

<my-component @click.native.prevent="doSomething"></my-component>

检查事件

于 2019-07-10T01:57:58.980 回答
2

你查过说明书吗?https://vuejs.org/v2/guide/events.html

@click.stop=""@click.stop.prevent=""

所以你不需要使用这个

methods: {
    clickHandler(e) {
      e.stopPropagation();
      console.log(e);
    }
  }
于 2018-03-20T16:37:20.800 回答
0

I had the same problem. I fixed the issue by using following:

<MyComponent @click.native.prevent="myFunction(params)" />
于 2019-04-22T16:20:35.117 回答