简体   繁体   English

Vue - 无法使用 $emit 事件将数据从组件传递到父级

[英]Vue - Unable to Pass Data From Component to Parent with $emit Event

Pass data from child to parent in Vuejs (is it so complicated?) 在 Vuejs 中将数据从子级传递给父级(有这么复杂吗?)

Can't pass data from children to parent component with emit 无法使用发射将数据从子组件传递到父组件

$emit an event from child to parent component Vue 2 $emit 一个事件从子组件到父组件 Vue 2

I've looked through the posts above and the Vue documentation.我浏览了上面的帖子和 Vue 文档。 From what I can see, I'm doing everything right, but it's still not working.从我所见,我做的一切都是正确的,但它仍然无法正常工作。

I've included my code but I'm afraid that I'm unable to a reproduction with a stack snippet.我已经包含了我的代码,但我担心我无法使用堆栈片段进行复制。 A working reproduction can be found here on this code sandbox可以在此代码沙箱上找到工作副本


Buttons.vue按钮.vue

I've noted below under the navigateTo() function that my console confirms that the function on the component is getting the correct value, but I'm not sure that the value is being properly emitted by the component or received by the parent.我在下面的navigateTo() function 下注意到,我的控制台确认组件上的 function 正在获取正确的值,但我不确定该值是否由组件正确发出或由父级接收。

<template>
  <div class="navigation section columns">
    <div class="container column has-text-centered" v-for="button in navigation" :key="button.text">
      <button class="button is-primary" @click="navigateTo(button)" type="button">{{ button.text }}</button>
    </div>
  </div>
</template>
<script>
export default {
  name: "Buttons",
  props: {
    text: String,
    dest: String
  },
  computed: {},
  methods: {
    navigateTo(button) {
      // This console log correctly outputs "button dest 2"(or whatever the button value is)
      console.log("button dest", button.dest);
      this.$emit("navigate", button.dest);
    }
  },
  data() {
    return {
      navigation: [
        { text: "North", dest: "2" },
        { text: "East", dest: "3" },
        { text: "South", dest: "4" },
        { text: "West", dest: "5" }
      ]
    };
  }
};
</script>

App.vue应用程序.vue

<template>
  <div id="app" class="container">
    <scene @navigate="navigateTo"></scene>
    <buttons></buttons>
  </div>
</template>

<script>
import Scene from "./components/Scene.vue";
import Buttons from "./components/Buttons.vue";

export default {
  name: "app",
  methods: {
    navigateTo(dest) {
      console.log('received value', dest);
      this.selectedScene = dest;
    }
  },
  components: {
    Scene,
    Buttons
  }
};
</script>
<style scoped>
</style>

The component emitting the event is the button and you are listening to the event on the scene component:发出事件的组件是按钮,您正在侦听场景组件上的事件:

Change your App.vue like this:像这样更改您的 App.vue:

<template>
  <div id="app" class="container">
    <scene ></scene>
    <buttons @navigate="navigateTo" ></buttons>
  </div>
</template>

<script>
import Scene from "./components/Scene.vue";
import Buttons from "./components/Buttons.vue";

export default {
  name: "app",
  methods: {
    navigateTo(dest) {
      console.log('received value', dest);
      this.selectedScene = dest;
    }
  },
  components: {
    Scene,
    Buttons
  }
};
</script>
<style scoped>
</style>

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

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