在原始代码中,无论是在if语句块还是else语句块中,都会执行以下两行代码:

this.form = {}
this.dialogFormVisible = false

这两行代码的作用是清空表单中的内容和关闭对话框。但是在if语句块或else语句块执行完之后,又会执行一次以下代码:

this.form = {}

这会导致表单中的内容再次被清空,所以在修改完内容后,表单中的内容并不会被保留。

要解决这个问题,可以将if语句块和else语句块中的以下代码:

this.form = {}

移动到if语句块和else语句块之外,即放在整个edit()函数的最后一行。这样就避免了重复清空表单中的内容,保证了修改完内容后表单中的内容不被清除。

修改后的代码如下:

edit() {
    if (this.form.id == undefined) {
        instance.post("/branchs", this.form).then(res => {
            if (res.date.code == 0) {
                instance.get("/branchs").then(res => {
                    this.tableData = res.data.data;
                })
                this.form = {}
            }
        })
    } else {
        instance.put("/branchs", this.form).then(res => {
            if (res.date.code == 0) {
                instance.get("/branchs").then(res => {
                    this.tableData = res.data.data;
                })
                this.form = {}
            }
        })
    }
    this.dialogFormVisible = false
    this.form = {}
}

这样修改后,点击修改按钮后,表单中的内容将会被正确清除。

edit if thisformid == undefined instancepostbranchs thisformthenres = if resdatecode == 0 instancegetbranchsthen

原文地址: https://www.cveoy.top/t/topic/i9At 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录