问题
I am implementing Google reCAPTCHA v3 with Angular 6.
<script src='https://www.google.com/recaptcha/api.js?render=KEY'></script>
Added script in index.html
In my AppComponent,
constructor(
@Inject(DOCUMENT) private document: any
) {
this.grecaptcha = this.document.grecaptcha;
}
and when i click form submit,
this.grecaptcha.execute('KEY', { action: 'action_name' })
.then(function (token) {
// Verify the token on the server.
console.log(token);
});
But,
ERROR TypeError: Cannot read property 'execute' of undefined
回答1:
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);
})
You can also try to install the typings @types/grecaptcha
, to get some type hinting to make your life a bit easier
来源:https://stackoverflow.com/questions/53761994/recaptcha-v3-not-working-angular-6-error-executing