0
点赞
收藏
分享

微信扫一扫

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


在写网页的吸顶效果时,出现错误:​​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 条评论