简体   繁体   English

Vue 父组件无法捕获子组件发出的事件

[英]Vue parent component unable to catch event emitted from the child

Child is emitting a custom event: Child 正在发出一个自定义事件:

<template>
  <div id="controls-container" class="controls-container">
    <div class="control-button icon-zoom-in" @click="zoomHandler('+')"></div>
    <div class="control-button icon-zoom-out" @click="zoomHandler('-')"></div>
    </div>
  </div>
</template>

<script>

export default {
    name: "ControlsContainer",
    methods: {
        zoomHandler(direction) {
            console.log("this will print");
            this.$emit('zoomHandler', direction);
        }
    }
};

</script>

Parent is not catching it:父母没有抓住它:

<template>
  <div id="map" ref="map" class="navigation-map">
    <controls-container @zoomHandler="zoom"></controls-container>
  </div>
</template>

<script>

import ControlsContainer from "./ControlsContainer.vue";

export default {
    name: "NavigationMap",
    components: { ControlsContainer },
    methods: {
        zoom(direction) {
            console.log("will not print");
            if (direction === "+") {
                this.map.zoomIn();
            } else if (direction === "-") {
                this.map.zoomOut();
            } else {
                // Do nothing
            }
        },
    },

</script>

I have now read about 7 tutorials on this and they all show how it's done in the exact same way I am doing it.我现在已经阅读了大约 7 篇关于这方面的教程,它们都展示了它是如何以与我完全相同的方式完成的。 Considering the hours I've wasted on this I really hope it isn't something really simple...考虑到我在这件事上浪费的时间,我真的希望这不是一件很简单的事情......

I noticed that the <script> tags are missing in your child component.我注意到您的子组件中缺少<script>标记。 Is that typo in your question?你的问题是那个错字吗? If they aren't there then that could explain the issue you're having.如果他们不在那里,那么这可以解释你遇到的问题。

Don't use Camel Case for DOM attributes, which applies to emitters and custom events as well.不要对 DOM 属性使用 Camel Case,这也适用于发射器和自定义事件。

In your child, refactor to:在您的孩子中,重构为:

this.$emit('zoom-handler', direction);

In your parent, refactor to:在您的父母中,重构为:

<controls-container @zoom-handler="zoom"></controls-container>

Working Example via codesandbox.io .通过代码和框的工作示例。io I had to open the preview in a new window/tab to display correctly.我必须在新窗口/标签中打开预览才能正确显示。

You can use this as your alternative.您可以使用它作为替代方案。

<template>
  <div id="controls-container" class="controls-container">
    <div class="control-button icon-zoom-in" @click="zoomHandler('+')"></div>
    <div class="control-button icon-zoom-out" @click="zoomHandler('-')"></div>
    </div>
  </div>
</template>

export default {
    name: "ControlsContainer",
    methods: {
        zoomHandler(direction) {
            console.log("this will print");
            this.$root.$emit('zoomHandler', direction);
        }
    }
};



<template>
  <div id="map" ref="map" class="navigation-map">
    <controls-container></controls-container>
  </div>
</template>

<script>

import ControlsContainer from "./ControlsContainer.vue";

export default {
    name: "NavigationMap",
    components: { ControlsContainer },
    methods: {
    },
    beforeDestroy(){
      this.$root.$off("zoomHandler")
    },
    mounted(){
      this.$root.$on("zoomHandler", (direction)=>{
        if (direction === "+") {
                this.map.zoomIn();
            } else if (direction === "-") {
                this.map.zoomOut();
            } else {
                // Do nothing
            }
      })
    }
</script>

These components are not nested, so Map is not actually the parent.这些组件没有嵌套,因此 Map 实际上不是父级。 Making it so causes some other issues.这样做会导致其他一些问题。 Not worh the hassle.不值得麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM