2017-03-31 160 views
3

因此,我學到了VueJS 2在Vue2中刪除了函數的順序:'(我一直試圖在Vue2中重寫我的應用程序並理解計算屬性是新的方法。 :Vue JS 2數據上的多個計算屬性

alphabeticalPosts: function() { 
    return _.orderBy(this.posts, 'name') 
}, 

searchedPosts: function() { 
    return this.posts.filter((post) => { 
     return 
      post.name.toLowerCase().includes(this.keyword.toLowerCase()); 
    }); 
}, 

兩個彼此分開,這些工作我遇到的問題是,我需要既對HTML像這樣:

<li v-for="post in searchedPosts/alphabeticalPosts">{{post.name}}</a> 

現在,當然,這並不工作,但它說明了我的問題,我需要將它們應用於相同的數據集:

ng-repeat="post in posts | searchedPosts | alphabetizePosts" 

當然,這也不適用於Vue。我需要將這兩個計算機屬性合併爲一個印象?我的JS缺乏,每次我嘗試這個時,結果都很糟糕。我如何去使用我的計算屬性?

回答

2
computed:{ 
    filteredAndOrdered: function() { 
     const keyword = this.keyword.toLowerCase(); 
     const filtered = this.posts.filter(post => post.name.toLowerCase().includes(keyword)); 
     return _.orderBy(filtered , 'name') 
    } 
} 

模板

<li v-for="post in filteredAndOrdered">{{post.name}}</a>