先监听输入框里面数据的变化,再通过接口对比value是否已存在,返回一个参数 this.checkCode,通过参数进行表单验证!
data() {
var validateName = async (rule, value, callback) => {
console.log(this.checkCode);
console.log(6666);
if (value === "") {
callback(new Error("必填!"));
} else if (this.checkCode === "error") {
callback(new Error("名称已存在,请重新输入!"));
}
};
watch: {
"form.name"(newVal) {
request({
url: "/api/admin/notify...?name=" + this.form.name,
method: "get",
})
.then((res) => {
console.log(res);
})
.catch((err) => {
this.checkCode = err;
console.log(err);
});
console.log(newVal);
},
},
菜鸟笔记:写的不好,欢迎指正,嘿嘿!