解决儿童发出事件后的承诺(VUE)

发布于 2025-02-10 12:41:48 字数 1086 浏览 0 评论 0原文

因此,用户提出了一些数据请求,但事先提示了验证码挑战...当解决挑战时,验证码组件会发出一个需要在请求中进行的令牌。

我的问题是:我能否利用承诺等待解决挑战的解决,然后用令牌提出请求?我可以听这样的儿童组件事件,然后解决诺言吗?

如果这不是处理这种情况的正确方法,有人可以将我指向正确的方向吗?谢谢!

我可以使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文