- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个计算以使“显示更多”按钮在总数据等于显示数据时消失。但在 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>
非常感谢。
最佳答案
ESLint
抛出此警告(或错误 - 取决于您的设置),因为您的 compulated
属性中存在不需要的副作用,即:您设置了 totalblog
组件的 data
属性。
计算值(尽管以函数形式呈现)不应更改其范围之外的任何内容。
不过,有几个解决方案可以解决您的问题:
如果这是警告,那么您可以接受。如果您确定需要这样做,则只需忽略该警告即可处理该警告。这不是一件好事,但警告就是警告:它们可能会导致问题,但仅此而已。
只需在产生问题的行上方添加 //eslint-disable-next-line vue/no-side-effects-in-compulated-properties
即可。 (更多关于此here。)
例如这样的事情:
<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>
关于javascript - 如何修复 vue/计算属性中的无副作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68017219/
这2个有什么区别?一个使用 SideEffect,另一个不使用。 “每次成功重组都会调用 SideEffect”,但如果没有 SideEffect,它也会在每次重组时运行。 @Composable f
我在 DOM 元素引用方面遇到了一些问题,我想我已经追踪到它与更新 innerHTML 有关。 在这个例子中,在第一次警告时,两个变量引用同一个元素,正如预期的那样。奇怪的是,在更新父元素(body)
如果有人问过这个问题,请原谅我,但我似乎找不到它。 我正在尝试创建一个数组并反转它(不使用反转)这段代码完美运行: function reverseArrayInPlace(array) { fo
如果 reflector 是正确的(我倾向于相信它是正确的),这就是 Any() 的实现: public static bool Any(this IEnumerable source) {
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
是否可以用 LINQ 中的 lambda 表达式替换 foreach 循环 (.Select))? List l = {1, 2, 3, 4, 5}; foreach (int i in l)
我在一本书上读到以下说法: n = ((i++) > (j)?(i++):(j)); 书上说假设i>j,n有一个意想不到的值,i增加了两次。 我不明白为什么n在这句话之后有一个期望值。 我读了很多关于
我对更改 LD_LIBRARY_PATH 有奇怪的副作用。 当我附加一个包含库的路径时,例如: LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/my_path/lib 然后,一切都
有人能告诉我下面一行中“副作用”的含义吗? If you're calling an EL function that doesn't return anything, then you're cal
是否有为包含副作用的 Java/JVM 语言方法编写 javadoc 的标准或最佳实践? 我定义了一个 void 方法,它修改了方法参数之一,但不知道如何记录实际返回值(因为没有实际返回)。 /**
我正在学习副作用和纯函数。我知道纯函数没有副作用,对于相同的参数,它们的返回值是相同的。我想知道 C 函数 strcmp() 是否是纯函数。我相信它是纯粹的,因为给定相同的两个字符串作为参数,结果将始
我正在尝试创建佛罗里达州的点密度图。虽然我知道 Highmaps 不支持带有 map 点的颜色轴。我扩展了它并且它有效,但它带来了副作用。当我单击图例中的某一类别时,不会发生隐藏。例如,如果我单击“>
我在 CS50 中研究 PSET 4,似乎遇到了 sprintf 更改不相关变量的问题。我只给出了没有揭示我的解决方案的代码...... #include #include #include t
我已经实现了这样的 UnaryOperation struct Converter { Converter( std::size_t value ): value_( valu
使用点符号调用自定义 getter 是否有副作用? 我一直在通过点符号在 Objective-C 中使用合成的 getter,即 tree.fruitnumber 返回树中果实的数量。我必须自定义 s
我无法理解页面 https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special/void 中的这一段: This ope
我有一个在 IIS7 下运行的 Web 应用程序。我将全局变量存储在一个带有静态变量的类中。该类称为 SessionVariables 并且在其中例如我有以下内容: public class Sess
运行命令时 ng-packagr -p ng-package.json 我得到以下输出 Building Angular library - - - skipped 8 lines - - - Sid
我想模拟一个 OverflowError 因为我想在引发异常之后测试变量的值。但是,我不知道如何使用我正在使用的库复制 OverflowError。我在此特定测试中使用的库是 pysolar.sola
当我尝试在可变 Map 中插入一个元素时,我希望这个元素插入到我的 Map 而不是返回 Map(如 PF,不可变对象(immutable对象) ecc ...)出于这个原因,我使用了可变集合,但在我的
我是一名优秀的程序员,十分优秀!