reCAPTCHA v3无法正常工作的角度6-执行错误 [英] reCAPTCHA v3 not working angular 6 - error executing

查看:177
本文介绍了reCAPTCHA v3无法正常工作的角度6-执行错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Angular 6实现Google reCAPTCHA v3.

I am implementing Google reCAPTCHA v3 with Angular 6.

<script src='https://www.google.com/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-执行错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆