本文介绍了reCAPTCHA v3无法正常工作的角度6-执行错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Angular 6实现Google reCAPTCHA v3.
I am implementing Google reCAPTCHA v3 with Angular 6.
<script src='www.google/recaptcha/api.js?render=KEY'></script>在index.html
在我的AppComponent中,
In my AppComponent,
constructor( @Inject(DOCUMENT) private document: any ) { this.grecaptcha = this.document.grecaptcha; }当我单击表单提交时,
this.grecaptcha.execute('KEY', { action: 'action_name' }) .then(function (token) { // Verify the token on the server. console.log(token); });但是
ERROR TypeError:无法读取未定义的属性"execute"
ERROR TypeError: Cannot read property 'execute' of undefined
推荐答案
该对象应该在window中可用,因此您所需要做的就是在ts文件顶部声明它:
the object should be available from window, so all you need is to declare it on top of your ts file:
declare const grecaptcha: any;然后您可以在班级中使用它,例如:
then you can use it in your class like:
grecaptcha.execute('KEY', { action: 'action_name' }) .then(function (token) { // Verify the token on the server. console.log(token); })您也可以尝试安装键入@types/grecaptcha,以获得一些类型提示,使您的生活更轻松
You can also try to install the typings @types/grecaptcha, to get some type hinting to make your life a bit easier
更多推荐
reCAPTCHA v3无法正常工作的角度6
发布评论