简体   繁体   English

Angular 6-更换页面时reCAPTCHA v2无法正常工作

[英]Angular 6 - reCAPTCHA v2 Not Working When I Change Pages

i'm creating a website using Angular 6. 我正在使用Angular 6创建一个网站

I've routed my whole website using the router-outlet. 我已经使用路由器出口路由了整个网站

I have 5 pages and out of these 5 pages 3 of pages has a contact form. 我有5页,这5页中有3页有联系表。 All the pages contain the same exact form. 所有页面都包含相同的确切形式。

My problem is that when I first load the website reCAPTCHA v2 works perfectly but when I change to a different page via navigation the reCAPTCHA v2 doesn't even appear unless I reload the whole page by clicking refresh. 我的问题是,当我第一次加载网站reCAPTCHA v2时,它运行良好,但是当我通过导航切换到其他页面时,除非单击刷新以重新加载整个页面,否则reCAPTCHA v2甚至不会出现。 Once it works on the page it wasn't working on when I change pages without refresh again i get the same problem. 一旦在页面上运行,当我再次更改页面而不刷新时就无法使用了,我遇到了同样的问题。 reCAPTCHA v2 only seems to be responding when I fully refresh the page. 当我完全刷新页面时,reCAPTCHA v2似乎才响应。

How would I resolve this on a single page application? 如何在单页应用程序上解决此问题?

I want to have the same form in multiple pages without refreshing the page. 我想在多个页面中使用相同的表单而不刷新页面。

This looks to be a duplicate of Recaptcha google on angular 6 implementation 这看起来像是Recaptcha Google在angular 6实现上的副本

That being said, depending on how you're implementing, there are plenty of npm/yarn modules you can use: 话虽如此,根据您的实现方式,您可以使用很多npm / yarn模块:

https://www.npmjs.com/search?q=angular%20recaptcha https://www.npmjs.com/search?q=angular%20recaptcha

https://yarnpkg.com/en/packages?q=angular%20recaptcha&p=1 https://yarnpkg.com/en/packages?q=angular%20recaptcha&p=1

Most packages have instructions included so you should be fine. 大多数软件包都包含说明,因此您应该没事。 If you go down the direct implementation route, you can add a custom validator to your forms and let Angular take care of the rest: 如果您采用直接实现路线,则可以在表单中添加自定义验证器,然后让Angular负责其余的工作:

function recaptchaValidator(control: FormControl): { [res: any]: boolean } {
    if (addYourServerSideLogicHere(control)) {
        return {validCaptcha: true};
    }
}

Custom validators are also well documented on the web...I'd suggest starting with a simple boolean validator and build your knowledge from there. 自定义验证器在网络上也有很好的文档记录...我建议从一个简单的布尔验证器开始,然后从那里建立您的知识。

There are also plenty of great articles on how to implement this, for example: 关于如何实现此功能的文章也很多,例如:

https://netbasal.com/how-to-integrate-recaptcha-in-your-angular-forms-400c43344d5c https://netbasal.com/how-to-integrate-recaptcha-in-your-angular-forms-400c43344d5c

Hope this helps and Happy coding! 希望对您有所帮助,并祝您编程愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM