【问题标题】:How to fix vue/no-side-effects-in-computed-properties?如何修复 vue/no-side-effects-in-computed-properties?
【发布时间】: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


【解决方案1】:

ESLint 抛出此警告(或错误 - 取决于您的设置),因为您的 computed 属性中存在不需要的副作用,即:您设置了组件的 totalblog data 属性。

计算值(尽管以函数形式表示)不应更改超出其范围的任何内容。

不过,有几个解决方案可以解决您的问题:

0。就这样吧

如果这是一个警告,那么你可以忍受它。如果你确定你想要这个,那么只需忽略它来处理警告。这不是一件好事,但警告就是警告:它们可能会导致问题,但仅此而已。

1。移除 ESLint 规则

只需在产生问题的行上方的行中添加// eslint-disable-next-line vue/no-side-effects-in-computed-properties。 (更多关于here。)

2。拥有更好的代码结构

这样的东西,例如:

<script>
export default {
  data() {
    filtereddata: {
      title: '',
      district: 'all'
    },
    showing: 2,
    sort: '',
  },
  computed: {
    totalblog() {
      return this.temp.length
    },
    temp() {
      if (this.filtereddata.district === 'all') {
        return this.properties.filter(x => x.title.toLowerCase().includes(this.filtereddata.title))
      } else {
        return this.properties.filter(x => x.title.toLowerCase().includes(this.filtereddata.title) && x.district === this.filtereddata.district)
      }
    },
    filteredproperties() {
      return this.sortProperties(this.temp)
    },
  },
  methods: {
    sortProperties(list) {
      let temp = [...list]
      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>

【讨论】:

    猜你喜欢
    • 2021-06-20
    • 2012-12-11
    • 2017-12-22
    • 2022-09-24
    • 1970-01-01
    • 2019-02-08
    • 2020-02-17
    • 1970-01-01
    • 2021-01-15
    相关资源
    最近更新 更多