if (this.ruleForm.id) { alarmClassifyUpdate(this.ruleForm).then((res => { if (res.code == 200) { this.$message({ type: 'success', message: '编辑成功!' }); this.getalarmlists() this.dialogVisible = false } else { this.$message('输入值有误,请检查后重试'); } })) } else { alarmConfigAdd(this.ruleForm).then((res => { if (res.code == 200) { this.$message({ type: 'success', message: '新增成功!' }); this.getalarmlists() this.dialogVisible = false } else { this.$message('输入值有误,请检查后重试'); } })) } 优化代码
时间: 2024-01-19 21:01:58 浏览: 31
你可以将相同的代码抽取出来,然后再使用三目运算符进行判断。优化后的代码如下:
```
const api = this.ruleForm.id ? alarmClassifyUpdate : alarmConfigAdd
api(this.ruleForm).then((res) => {
if (res.code == 200) {
this.$message({
type: 'success',
message: this.ruleForm.id ? '编辑成功!' : '新增成功!'
});
this.getalarmlists()
this.dialogVisible = false
} else {
this.$message('输入值有误,请检查后重试');
}
})
```
相关问题
const api = this.ruleForm.id ? alarmProtocolUpdate : alarmProtocolAdd api(this.ruleForm).then((res => { if (res.code !== 200) { return false } this.$message({ message: this.ruleForm.id ? '编辑成功' : '添加成功', type: 'success' }); this.close() this.$emit("ben_can") })) 如何不管this.ruleforem 有没有都执行 第一个
你可以使用逻辑非运算符 `!` 来判断 `this.ruleForm.id` 是否为 `undefined` 或 `null`,如果是,则将其赋值为一个空对象,即 `{}`。这样在后面的三目运算符中,`this.ruleForm.id` 就会被认为是存在的,然后执行 `alarmProtocolAdd` 方法。
修改后的代码如下:
```
const api = (!this.ruleForm.id ? alarmProtocolAdd : this.ruleForm.id ? alarmProtocolUpdate : alarmProtocolAdd)
api(this.ruleForm).then((res => {
if (res.code !== 200) { return false }
this.$message({
message: this.ruleForm.id ? '编辑成功' : '添加成功',
type: 'success'
});
this.close()
this.$emit("ben_can")
}))
```
methods: { async init () { queryEmployeeById(this.id).then(res => { console.log(res) if (String(res.code) === '1') { console.log(res.data) this.ruleForm = res.data this.ruleForm.sex = res.data.sex === '0' ? '女' : '男' // this.ruleForm.password = '' } else { this.$message.error(res.msg || '操作失败') } }) }
这个问题属于技术问题,我可以回答。这个方法是一个异步方法,用于初始化数据。首先通过id查询员工信息,如果查询成功并且返回code为1,则将查询到的数据赋值给ruleForm对象,并将性别从数字转换为文字。如果查询失败则输出错误提示信息。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)