解决儿童发出事件后的承诺(VUE)
因此,用户提出了一些数据请求,但事先提示了验证码挑战...当解决挑战时,验证码组件会发出一个需要在请求中进行的令牌。
我的问题是:我能否利用承诺等待解决挑战的解决,然后用令牌提出请求?我可以听这样的儿童组件事件,然后解决诺言吗?
如果这不是处理这种情况的正确方法,有人可以将我指向正确的方向吗?谢谢!
我可以使用v-on:验证的
来收听儿童组件事件,但是我想在提出请求时触发挑战,然后在用户解决验证码后继续进行挑战。
//component.ts
import captchaComponent from '...';
@Component({
components: {captchaComponent}
})
@Ref() readonly captcha!: captcha;
function executeCaptchaChallenge(){
// executes challenge on child component via boolean prop
this.runChallenge = true;
const vm = this;
return new Promise(resolve => {
vm.captcha.$on('verified', token => {resolve(token)}
})
}
function async getData(){
const token = await this.executeCaptchaChallenge();
const response = await axios.post(URL, token);
}
///component.vue
<template>
<div>
<button @click="getData">GET DATA</button>
<captchaComponent ref="captcha" />
</div>
</template>
<script src="component.ts"></script>
So the user makes a request for some data but its prompted with a captcha challenge beforehand... when the challenge is resolved, the captcha component emits an event with a token that needs to go in the request.
My question is: can I use a promise to wait for the challenge to be resolved and then make the request with the token? Can I listen to a child component event like this and then resolve the promise?
If this is not the correct way to handle such situation could anyone point me in the right direction? Thanks!
I can listen to the child component event with a v-on:verified
but I want to trigger the challenge when making the request and then continue with it after the captcha is resolved by the user.
//component.ts
import captchaComponent from '...';
@Component({
components: {captchaComponent}
})
@Ref() readonly captcha!: captcha;
function executeCaptchaChallenge(){
// executes challenge on child component via boolean prop
this.runChallenge = true;
const vm = this;
return new Promise(resolve => {
vm.captcha.$on('verified', token => {resolve(token)}
})
}
function async getData(){
const token = await this.executeCaptchaChallenge();
const response = await axios.post(URL, token);
}
///component.vue
<template>
<div>
<button @click="getData">GET DATA</button>
<captchaComponent ref="captcha" />
</div>
</template>
<script src="component.ts"></script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论