我正在使用Vue 2创建可根据类别排序的帖子列表 . 类别选择输入是我正在使用自定义组件的按钮 . 目标是根据点击的类别过滤帖子列表(每个帖子可以属于多个类别) .
这在使用filteredBy的Vue 1中非常简单,但我在使用计算属性的Vue 2中失败了 .
Vue实例:
const port = new Vue( {
el: '#portfolio-app',
data: {
portfolio: {},
portfolioCats: {},
portfolioLoading: true,
categorySort: ''
},
created: function() {
let $this = this;
//2 requests will be sent: 1 one for the CPT, and one for the custom taxonomy
let request = new XMLHttpRequest();
let catsRequest = new XMLHttpRequest();
//The request for the CPT
request.onload = function() {
$this.portfolio = JSON.parse( request.responseText );
$this.portfolioLoading = false;
};
request.onerror = function() {
console.log( 'A connection error has occurred' );
};
request.open( 'GET', endPoint, true );
request.send();
//The request for the custom taxonomy
catsRequest.onload = function() {
$this.portfolioCats = JSON.parse( catsRequest.responseText );
};
catsRequest.onerror = function() {
console.log( 'A connection error occurred' );
};
catsRequest.open( 'GET', catsEndPoint, true );
catsRequest.send();
},
computed: {
categorySort: function () {
let $self = this;
return $self.portfolio.filter( ( portfolio ) => {
return portfolio.portfolio_category.match( $self.categorySort );
} );
}
}
} );
排序按钮组件:
Vue.component( 'sort-button', {
props: ['value', 'cat'],
template: `
<input type="button" class="button sort-button button-accent"
v-bind:value="cat.name"
v-on:input="$emit( 'input', $event.target.value )"
>
`
}
);
模板:
<div class="portfolio-categories-wrapper">
<sort-button v-for="cat in portfolioCats" :cat="cat" v-model="categorySort"></sort-button>
</div><!--ends portfolio-categories-wrapper-->
<div class="portfolio-wrapper">
<figure class="portfolio-item" v-for="post in categorySort">
<a v-bind:href="post.link">
<img v-bind:src="post._embedded['wp:featuredmedia'][0].media_details.sizes.medium.source_url">
</a>
<figcaption class="portfolio-caption"><a v-bind:href="post.link"><span v-html="post.title.rendered"></span></a></figcaption>
</figure>
</div><!--ends portfolio-wrapper-->
请注意,计算属性中的“portfolio_category”是每个项目组合项中的整数数组 .
这样可以避免错误,但不会显示任何项目 .
任何帮助表示赞赏 .
1 回答
基于另一个动态列表过滤列表的关键是确保后者是被动的 . 见Reactivity in Depth .
这是一个快速演示,按选定的类别过滤项目: