Vue中同步方法的实现
情景:在实现登录功能的时候,通过表单的用户名,向后台发送请求,前端以为处理完成,继续执行,而还未等后台数据返回,前端获取到的用户数据为空。
实现:等待请求方法返回数据在继续往下执行,及实现同步方法
原代码
data() {
return {
userInfo: {
id: '',
username: '',
password: '',
avatar: '',
},
}
}
methods:{
getUserInfo: function () {
let _this = this;
this.axios({
url: "http://localhost:8088/verifyLogin",
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
method: "post",
params: {