【发布时间】:2021-09-02 03:10:25
【问题描述】:
我创建了一个计算来使“显示更多”按钮在总数据等于显示数据时消失。 但是在 VueJS v3.6 引入了这个 ESlint 规则之后,我找不到任何方法来解决这个问题。我该如何解决?
<template>
<div class="row sorting">
<select v-model='sort' class="form-control" id="sorting">
<option value="">Default</option>
<option value="rentlow">Rent (ascending)</option>
<option value="renthigh">Rent (descending)</option>
<option value="arealow">Area (ascending)</option>
<option value="areahigh">Area (descending)</option>
</select>
</div>
上面是排序属性,下面是axios API调用数据。我使用 v-for。
<div class="row">
<div v-for='(property, idx) in filteredproperties' :key='idx' class='col-md-6 text-left'>
<img v-if='property.thumbnail' :src='backendurl + property.thumbnail.url'>
<img v-if='!property.thumbnail' src='pictures/inner.png'>
<h4>${{ property.rent }} / mo</h4>
<h3>{{ property.title }}</h3>
<span> {{ property.address }}</span>
<div>{{ property.area }}
<br>{{ property.room }}
<br>{{ property.livingroom }}
<br>{{ property.date }}
</div>
<button @click='showmore' v-if='totalblog > showing'>Show More</button>
</div>
<form>
<div class="form-group">
<label for="exampleFormControlInput1">address</label>
<input type="text" v-model='filtereddata.title' class="form-control" id="exampleFormControlInput1">
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">District</label>
<select class="form-control" v-model='filtereddata.district' id="exampleFormControlSelect1">
<option value="all"> All </option>
<option value="dis1">District 1</option>
<option value="dis2">District 2</option>
<option value="dis3">District 3</option>
</select>
</div>
<button type="submit" class="btn btn-primary mt20">Search</button>
</form>
</template>
控制过滤器、排序和按钮的javascript。问题是我不能使用“this.totalblog = temp.length”,因为它表示意外的副作用。我不知道如何在加载所有数据后消失显示更多按钮。有什么选择吗?
<script>
export default {
data () {
return {
frontendurl: process.env.frontendurl,
backendurl: process.env.backendurl,
filtereddata: {
title: '',
district: 'all'
},
sort: '',
showing: 2,
totalblog: ''
}
},
async asyncData ({ $axios }) {
const properties = await $axios.$get(process.env.backendurl + '/properties')
return { properties }
},
methods: {
showmore () {
this.showing = this.showing + 2
}
},
computed: {
filteredproperties () {
let temp = ''
if (this.filtereddata.district === 'all') {
temp = this.properties.filter(x => x.title.toLowerCase().includes(this.filtereddata.title))
} else {
temp = this.properties.filter(x => x.title.toLowerCase().includes(this.filtereddata.title) && x.district === this.filtereddata.district)
}
this.totalblog = temp.length /* unexpected side effect */
/* sorting algorithm */
if (this.sort === 'rentlow') {
temp.sort((a, b) => {
return a.rent - b.rent
})
}
if (this.sort === 'renthigh') {
temp.sort((a, b) => {
return b.rent - a.rent
})
}
if (this.sort === 'arealow') {
temp.sort((a, b) => {
return a.area - b.area
})
}
if (this.sort === 'areahigh') {
temp.sort((a, b) => {
return b.area - a.area
})
}
return temp.slice(0, this.showing)
}
}
}
</script>
非常感谢。
【问题讨论】:
-
不做
this.totalblog = temp.length来修复它——不知道你还能做什么——除了让filteredproperties成为一个方法而不是一个计算方法
标签: javascript vue.js computed-properties side-effects