javascript - vuejs如何在后台运行方法
问题描述
我有如下登录表单。工人可以轻松地首次登录和注销。当工作人员注销并立即尝试再次登录并且我得到错误漂移未定义时,问题就出现了。我想在代码的漂移部分添加延迟,但我不希望工作人员等待延迟再次被记录。
所以像 10 秒或更长时间的延迟,但工作人员不应该等待登录。继续在后台检查漂移并运行漂移部分,但它不应该冻结登录。帮我实现这个
<template>
<v-layout row wrap>
<v-flex xs12 md7 lg8 >
<v-layout row wrap>
<v-flex md12 lg12>
<div>
<v-form :model='worker' ref='loginForm'>
<v-layout row wrap>
<v-flex xs12 md6>
<v-text-field
label='Email Address'
v-model='worker.email'
name="email"
:rules='emailRules'
required>
</v-text-field>
</v-flex>
<v-flex xs12 md6>
<v-text-field
label='Password'
v-model='worker.password'
name="password"
type='password'
:rules='passwordRules'
required>
</v-text-field>
</v-flex>
</v-layout>
<v-btn @click='login'>Log In</v-btn>
</v-form>
</div>
</v-flex>
</v-layout>
</v-flex>
</v-layout>
</template>
<script>
export default {
data: function() {
return {
worker: {
email: '',
password: ''
},
error: '',
emailRules: [
value => !!value || 'Please enter your email address',
],
passwordRules: [
value => !!value || 'Please enter a password'
]
};
},
methods: {
login() {
var that = this;
if (this.$refs.loginForm.validate()) {
this.$axios.post('sign_in.json', { user: this.worker }).then(function(response) {
drift.identify(response.data.worker.id, {
email: response.data.worker.email
})
window.location.href = '/my_account';
}).catch(function(error) {
that.error = error;
});
}
}
}
};
</script>
解决方案
我不熟悉聊天框/漂移,所以我不知道你是如何获得“漂移”实例的,但我认为你将不得不为“漂移”的可用性添加某种异步检查例如,像
login() {
var that = this;
if (this.$refs.loginForm.validate()) {
this.$axios.post('sign_in.json', { user: this.worker }).then(async function(response) {
await getDrift().identify(response.data.worker.id, {
email: response.data.worker.email
})
window.location.href = '/my_account';
}).catch(function(error) {
that.error = error;
});
}