我有在父組件中生成的事件,並且孩子必須對它作出反應。我知道這不是vuejs2
的推薦方法,我必須做一個$root
發射,這是非常糟糕的。所以我的代碼是這樣的。來自父母組件的事件
<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
被觸發無限滾動和事件被髮送到子組件的搜索。那麼不只是搜索,而是因爲它的根源,它正在向每個人播放。
這是什麼更好的方法。我知道我應該使用道具,但我不確定在這種情況下我該怎麼做。
節你可以使用這個命令直接在孩子身上發射:$ children [index]。$ emit:https://vuejs.org/v2/api/#vm-children – Potray
Search組件是孩子嗎?一個道具? –
@RoyJ是搜索是一個孩子,你可以在這裏看到的代碼我在行號2提供 模板,我可以添加一個道具,但這是我的問題我如何使用道具來發出loadMore? –
madeye