Javascript 在vue.js中按类型对注释排序
我有一个带有选择过滤器的组件窗体:Javascript 在vue.js中按类型对注释排序,javascript,vue.js,vuejs2,Javascript,Vue.js,Vuejs2,我有一个带有选择过滤器的组件窗体: <template> <div class="form-group"> <select name="" v-model="filterRev" @change="filterReviews(filterRev)" class="form-control" id=""> <option value="0">All com
<template>
<div class="form-group">
<select name="" v-model="filterRev" @change="filterReviews(filterRev)" class="form-control" id="">
<option value="0">All comments</option>
<option value="1">Good Comments</option>
<option value="2">Standard Comments</option>
<option value="3">Badd comment</option>
</select>
</div>
</template>
<script>
export default {
data() {
filterRev: 0
},
methods: {
filterReviews(type) {
if(Number.isInteger(parseInt(type))) {
this.$emit('filter', type);
}
},
}
}
</script>
所有评论
好评论
标准评论
添加评论
导出默认值{
数据(){
filterRev:0
},
方法:{
filterReviews(类型){
if(Number.isInteger(parseInt(type))){
此.$emit('filter',type);
}
},
}
}
关于组件评论,我有以下几点:
<div @filter="..." v-for="(comment, index) in items" :key="comment.id">
<comment :data="comment"></comment>
</div>
如何使用
过滤器类型检查注释.类型
?我需要排序的意见,当用户选择某些过滤器。在v-for
中,我有注释。键入处理此类过滤器的一个好方法是使用。这是一个进步,但在更复杂的场景中肯定会对您有很大帮助。一个解决方案是使用数据变量进行筛选选择,并使用计算属性返回筛选后的注释:
在容器组件中创建一个数据变量,以保存选定的过滤器类型(例如,filterType
)。由于来自选择组件的过滤器
事件在事件数据中发出过滤器ID(可通过模板获得),因此我们可以使用它在@filter
-事件处理程序中设置过滤器类型
:
// template
<filter-select @filter="filterType = $event" />
// script
data() {
return {
filterType: 0
}
}
在v-for
循环中引用该计算属性:
<comment v-for="comment in comments" :key="comment.id" :data="comment" />
<comment v-for="comment in comments" :key="comment.id" :data="comment" />