我有一个在父组件中生成的事件,子组件必须对其做出反应。我知道这不是vuejs2
推荐的方法,我必须进行一个$root
emit,这非常糟糕。所以我的代码是这样的。
<template>
<search></search>
<table class="table table-condensed table-hover table-striped" v-infinite-scroll="loadMore" infinite-scroll-disabled="busy" infinite-scroll-distance="10">
<tbody id="trans-table">
<tr v-for="transaction in transactions" v-model="transactions">
<td v-for="item in transaction" v-html="item"></td>
</tr>
</tbody>
</table>
</template>
<script>
import Search from './Search.vue';
export default {
components: {
Search
},
data() {
return {
transactions: [],
currentPosition: 0
}
},
methods: {
loadMore() {
this.$root.$emit('loadMore', {
currentPosition: this.currentPosition
});
}
}
}
</script>
正如您所看到的,loadMore
在无限滚动时被触发,并且事件被发送给子组件“search”。不仅是搜索,因为它是根组件,所以它会广播给所有人。
这种情况下有更好的方法。我知道我应该使用props,但我不确定如何在这种情况下做到这一点。