request里对外部的info赋值不成功!!
import {request} from "../network/request/request";
export function CententCheck(mail, title) {
const info = null;
request({
url: 'CententCheck',
params: {
mail: mail,
title: title,
}
}).then(res => {
if (res.data.success) {
if (res.data.data.length >= 1) {
this.info = true;
} else {
this.info = false;
}
} else {
console.log('数据库错误');
}
}).catch(err => {
console.log('api错误' + err);
})
return info;
}
异步操作需要用回调的形式获取数据,然后需要用到数据的代码放到回调里面。改成下面这种模式
import { request } from "../network/request/request";
export function CententCheck(mail, title,callback) {
const info = null;
request({
url: 'CententCheck',
params: {
mail: mail,
title: title,
}
}).then(res => {
if (res.data.success) {
if (res.data.data.length >= 1) {
this.info = true;
} else {
this.info = false;
}
callback(this.info);///
} else {
console.log('数据库错误');
}
}).catch(err => {
console.log('api错误' + err);
})
//return info;//没意义,除非request能改为同步请求,否则这里info永远为null
}
//调用CententCheck
CententCheck('mailxxxx', 'titlexxxx', function (info) {
console.log(info)
})
异步请求,请求要时间的嘛,你还没请求完就开始返回了肯定得不到结果啊。
可以学习一下我的博客了解一下异步的解决方案。
js Promise与async/await用法详解:https://blog.csdn.net/weixin_43877799/article/details/121536943