【发布时间】:2019-12-08 06:23:42
【问题描述】:
我在模板中使用 v-for 来显示计算属性的列表。但即使它在我刷新时工作,当我第一次进入页面时,我也看不到列出的项目。如果我 v-for 我的联系人,我无法过滤。
所以我的数据和计算方法如下所示:
export default {
data() {
return {
contacts: this.$store.getters.loadedContacts,
search: ""
};
},
computed: {
filterContacts(contacts) {
return this.contacts.filter(contact => {
return contact.name.toLowerCase().match(this.search.toLowerCase());
});
}
};
我在我的 HTML 中这样称呼它(filterContacts):
<v-list two-line v-if="contacts">
<template v-for="(contact, index) in filterContacts">
<v-list-tile-content>
<v-list-tile-title>{{ contact.name }}</v-list-tile-title>
<v-list-tile-action-text>{{ contact.position }}</v-list-tile-action-text>
</v-list-tile-content>
</template>
</v-list>
所以实际的问题是:为什么需要刷新我的页面才能看到 for 循环的结果?
如果我不调用 filterContacs,我将无法使用我的过滤器。
对如何解决过滤和 v-for 循环有什么建议吗?
感谢和抱歉,如果这是一个新手! 非常感谢任何帮助
【问题讨论】:
-
尝试用
this.$store.getters.loadedContacts替换this.contacts.filter。 -
另外,你可以在不同的地方添加虚拟数据,看看它在哪里下降。
标签: vuejs2 computed-properties v-for