Vue报错:Unnecessary use of boolean literals in conditional expression

阅读 69

2022-07-13


在写网页的吸顶效果时,出现错误:​​Unnecessary use of boolean literals in conditional expression​

data(){
return {
isFixed: false
}
},
mounted(){
window.addEventListener('scroll', this.initHeight)
},
destroyed(){
window.removeEventListener('scroll', this.initHeight)
},
methods: {
initHeight(){
var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
this.isFixed = scrollTop > 152 ? true : false; //错误在这一行
}
}

原因是Eslint的检查,说是如果有更简单的方案时,不允许使用三元运算符:

Vue报错:Unnecessary use of boolean literals in conditional expression_debug

Vue报错:Unnecessary use of boolean literals in conditional expression_三元运算符_02


把错误行改成以下代码即可:

this.isFixed = scrollTop > 152;


精彩评论(0)

0 0 举报