javascript - Vue.js $emit not being received by parent - Stack Overflow

I am having difficuly working out why the following code code is not working for a child and parent sin

I am having difficuly working out why the following code code is not working for a child and parent single page ponents.

Child.vue

<script>
export default {
  name: 'ChildComponent',
  data () {
      return {}
  }
  mounted: function() {
      this.emitSignal()
  },
  methods: {
    emitSignal: function () {
      console.log('>>emitSignal()')
      this.$emit('my_signal')
    }
  }
}
</script>

Parent.vue

<template>
  <div>
        <child-ponent v-on:my_signal="doSomething"></child-ponent>
  </div>
</template>
<script>
import ChildComponent from './ChildComponent.vue'
export default {
    name: 'ParentComponent',
    data () {
      return {
      }
    },
    ponents: {
      ChildComponent
    },
    methods: {
      doSomething: function () {
        console.log('doSomething')
      }
    }
  }
</script>

If I run the application, I see the child emits the signal >>emitSignal() in console, but the parent function 'doSomething' does not run.

Any idea what I could be missing?

NB I also tried an event bus as per here: /, the child fires but again the parent does not intercep the signal and run the specified function.

I am having difficuly working out why the following code code is not working for a child and parent single page ponents.

Child.vue

<script>
export default {
  name: 'ChildComponent',
  data () {
      return {}
  }
  mounted: function() {
      this.emitSignal()
  },
  methods: {
    emitSignal: function () {
      console.log('>>emitSignal()')
      this.$emit('my_signal')
    }
  }
}
</script>

Parent.vue

<template>
  <div>
        <child-ponent v-on:my_signal="doSomething"></child-ponent>
  </div>
</template>
<script>
import ChildComponent from './ChildComponent.vue'
export default {
    name: 'ParentComponent',
    data () {
      return {
      }
    },
    ponents: {
      ChildComponent
    },
    methods: {
      doSomething: function () {
        console.log('doSomething')
      }
    }
  }
</script>

If I run the application, I see the child emits the signal >>emitSignal() in console, but the parent function 'doSomething' does not run.

Any idea what I could be missing?

NB I also tried an event bus as per here: https://alligator.io/vuejs/global-event-bus/, the child fires but again the parent does not intercep the signal and run the specified function.

Share Improve this question asked Apr 25, 2017 at 7:25 proximacentauriproximacentauri 1,8796 gold badges29 silver badges55 bronze badges 1
  • 1 Use this.$parent.$emit('my_signal') instead. – Potray Commented Apr 25, 2017 at 7:28
Add a ment  | 

1 Answer 1

Reset to default 2

Your emit code is fine, besides a ma missing after data block in Child.vue

Working example: https://jsfiddle/63t082p2/42/

<div id="app">
  <child v-on:my_signal="doSomething"></child>
</div>

new Vue({
    el: '#app',
    methods: {
    doSomething: function () {
      console.log('doSomething')
    }
  },
  ponents: {
    'child' : {
      mounted: function() {
          this.emitSignal()
      },
      methods: {
        emitSignal: function () {
          console.log('>>emitSignal()')
          this.$emit('my_signal')
        }
      }
    }
  }
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745169075a4614812.html

相关推荐

  • javascript - Vue.js $emit not being received by parent - Stack Overflow

    I am having difficuly working out why the following code code is not working for a child and parent sin

    9小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信