简体   繁体   English

如何从其他两个计算道具制作计算道具?

[英]How to make a computed prop from two other computed props?

My task is to make pagination and search. 我的任务是进行分页和搜索。 I solved this problem by creating two computed properties. 我通过创建两个计算属性来解决这个问题。 Separately, they work correctly, but I need to unite them. 另外,它们正常工作,但我需要将它们联合起来。 I can't figure out how to do this? 我无法弄明白该怎么做?

  paginatedData() {
    const start = this.page * 10;
    const end = start + 10;
    return this.posts.slice(start, end);
  },
  filteredPosts() {
    return this.posts.filter((post) => {
      return post.title.match(this.search);
    });
  },

You can combine the properties by using the first as the basis for the second. 您可以使用第一个作为第二个的基础来组合属性。 For example, if you want to filter the posts and then paginate 例如,如果要过滤帖子然后分页

paginatedData() {
    const start = this.page * 10;
    const end = start + 10;
    return this.filteredPosts.slice(start, end);
},
filteredPosts() {
    return this.posts.filter((post) => {
      return post.title.match(this.search);
    });
},

Or if you want to paginate and then filter 或者,如果您想分页然后过滤

paginatedData() {
    const start = this.page * 10;
    const end = start + 10;
    return this.posts.slice(start, end);
},
filteredPosts() {
    return this.paginatedData.filter((post) => {
      return post.title.match(this.search);
    });
},

In your template, use whichever computed property is your desired ultimate output ( paginatedData in the first case, filteredPosts in the second). 在您的模板中,使用您想要的最终输出的计算属性(第一种情况下为paginatedData ,第二种情况下为filteredPosts )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM