js代码简化if判断,三元运算符与return
有三元运算符可以很好的代替if else简单语句
但是在使用的时候发现 与 return使用的时候 需要用这种形式
错误形式: val ? return 1 ? return 0;
正确形式: return val ? 1 : 0;
示例:
vue
searchQuery() {
if (this.applyReason == undefined) {
this.$message.warning('请选择查询类型')
return
}
this.getRecord()
},
getRecord(){}
简化后:
searchQuery() {
return this.applyReason == null ? this.$message.warning('请选择查询类型') : this.getRecord()
},
getRecord(){}

能一行写完绝不多行!
推荐:js一行If … else … else if语句

浙公网安备 33010602011771号