每当事件总线接收到新数据时,就会调用计算函数。如何重新渲染组件,以便显示新数据?
<template lang="pug">
div
div(v-for='user in filtered')
p {{ user.name }}
</template>
<script>
computed: {
filtered() {
// filter by using data
return this.users.filter((user) =>
return this.data.includes(user.name)
);
}
},
created() {
EventBus.$on('data', (data) => {
this.data = data;
}
},
data() {
return {
data: [],
users: [
// users
]
}
}
</script>
[要检测是否需要渲染,Vue checks references of objects and arrays,而不是其值或属性。