我正在使用角度6的primeng验证码,我有一个问题.当我尝试直接加载具有验证码确认的页面时出现此错误:
ERROR TypeError:window.grecaptcha.render不是Captcha.push ../ node_modules/primeng/components/captcha/captcha.js.Captcha.init(captcha.js:42)中Captcha.push ../ node_modules /的函数primeng/components/captcha/captcha.js.Captcha.ngAfterViewInit(captcha.js:32)
我只在index.html初始化了验证码:
<script src="https://www.google.com/recaptcha/api.js?render=explicit" async defer></script>
Run Code Online (Sandbox Code Playgroud)
但是如果我从另一个页面路由到此页面,它就可以工作.
任何想法如何解决?
我明白了,所以我要做的就是将window.grecaptcha对象附加到我的组件上,然后验证*ngIf码直到grecaptcha.render是真的。检查。
// In your component
ngOnInit(){
this.recaptcha = (window as any).grecaptcha;
}
// And then in your template
<ng-container *ngIf="recaptcha.render">
<p-captcha ...></p-captcha>
</ng-container>
Run Code Online (Sandbox Code Playgroud)
这样做,验证码组件将不会运行,直到脚本执行完毕并且该render函数存在于grecaptcha全局脚本中为止。
干杯。凡发现此问题的人,祝您好运。
| 归档时间: |
|
| 查看次数: |
840 次 |
| 最近记录: |