为什么Vue事件无法传播?

时间:2018-12-22 22:17:17

标签: javascript vuejs2

从子组件发出事件时,包含的应用程序为什么不捕获该事件?请参阅下面的代码,获取一个非常基本的示例。当单击链接之一时,我希望收到针对“ app”的警报,然后是针对“ component”的警报。相反,它仅给出“组件”。

Vue.component('list-item', {
  template: '<li><a href="#" v-on:click.prevent="itemClick">click</a></li>',
  methods: {
    itemClick() {
      this.$emit('item-event');
      alert('component');
    }
  }
});

new Vue({
  el: "#app",
  data: {
    items: [
      'a', 'b', 'c'
    ]
  },
  methods: {
    itemClick: function() {
      alert('app');
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app" v-on:item-event="itemClick">
  <ol>
    <li is="list-item" v-for="item in items"></li>
  </ol>
</div>

如果这不是从孩子到父母沟通的适当方式,那是什么?

1 个答案:

答案 0 :(得分:1)

Vue事件没有在DOM树中冒泡。您需要在发出事件的Sub loop() Dim timelist As Variant, sht as worksheet, rcell As Range, rrow As Range, t As Integer timelist = Array("Created", "Creation") For Each sht In ActiveWorkbook.Worksheets Set rrow = Range("A1.Z1") For t = LBound(timelist) To UBound(timelist) For Each rcell In rrow rcell.Select if rcell.Value = timelist(t) Then ActiveCell.Offset(1, 0).Range("A1").Select Range(Selection, Selection.End(xlDown)).Select Selection.NumberFormat = "dd/mm/yyyy hh:mm:ss" End If Next rcell Next t Next sht End Sub 组件上监听事件:

list-item
Vue.component('list-item', {
  template: '<li><a href="#" v-on:click.prevent="itemClick">click</a></li>',
  methods: {
    itemClick() {
      this.$emit('item-event');
      alert('component');
    }
  }
});

new Vue({
  el: "#app",
  data: {
    items: [
      'a', 'b', 'c'
    ]
  },
  methods: {
    itemClick: function() {
      alert('app');
    }
  }
});