让你的代码变漂亮吧
多层嵌套
this.axios.get(url).then(res=>{
if(res.status){
if(res.status==1){
this.isShow = true
}
}
})
this.axios.get(url).then(res=>{
res.status && res.status==1 && (this.isShow=true)
})
三目运算符
if(this.val==1){
Toast('提交成功')
}else{
Toast('提交失败')
}
let msg = this.val==1?'提交成功':'提交失败';
Toast(msg);
赋值
if(this.default){
this.index = this.default
}else{
this.index = 1
}
this.index = this.default || 1
多种情况弹窗处理
showMsg(type,msg){
let that = this;
if(type=='1'){
this.$dialog.confirm({
title:'提示',
cancelButtonText:'再来一次',
confirmButtonText:'aaaaa',
showCancelButton:false,
message:msg,
}).then(()=>{
this.aaa().call(that)
}).catch(()=>{
this.isShow = true
})
}
if(type=='2'){
return
}
if(type=='3'){
this.$dialog.confirm({
title:'提示',
cancelButtonText:'再来一次',
confirmButtonText:'ccccc',
showCancelButton:true,
message:msg,
}).then(()=>{
this.ccc().call(that)
}).catch(()=>{
this.isShow = true
})
}
if(type=='4'){
this.$dialog.confirm({
title:'提示',
cancelButtonText:'再来一次',
confirmButtonText:'ddddd',
showCancelButton:false,
message:msg,
}).then(()=>{
this.ddd().call(that)
}).catch(()=>{
this.isShow = true
})
}
}
showMsg(type,msg){
if(type==='2')return
let that = this;
let typeText = newMap([
[1,["aaaaa"],false,()=>this.aaa()],
[3,["ccccc"],false,()=>this.ccc()],
[4,["ddddd"],false,()=>this.ddd()],
])
this.$dialog.confirm({
title:'提示',
cancelButtonText:'再来一次',
confirmButtonText:typeText.get(type)[0],
showCancelButton:typeText.get(type)[1],
message:msg,
}).then(()=>{
typeText.get(type)[2].call(that)
}).catch(()=>{
this.isShow = true
})
}
参数默认值
function newParams(name){
const userName = name || '用户'
}
function newParams(userName='用户'){
}
操作数组
var addItem = (arr,item)=>{
arr.push({item,date: Date.now()})
}
var addItem = (arr,item)=>{
return [...arr, {item,date: Date.now()} ]
}