【问题标题】:How can i use recaptcha v3 in Angular?我如何在 Angular 中使用 recaptcha v3?
【发布时间】:2021-04-25 20:01:54
【问题描述】:

我知道那里有一些 ng 验证码库。我不想只通过一个方法调用来安装 npm 依赖项。在纯 java 脚本实现中,例如 onSubmit 方法调用,当我们提交表单时,我们从 google recaptcha api 调用该方法

 recaptcha.execute(secret_key, {action: 'homepage'}).then(function(token) {
       c.log('token', token);
})

但我找不到方法,我如何告诉 angular 和 typescript 这个 recaptcha 变量来自 google api,它将执行 execute 方法。

现在我当然得到了错误

 recaptcha.execute is not a function

somoene 找到了没有图书馆的方法吗?

【问题讨论】:

    标签: angular typescript recaptcha-v3


    【解决方案1】:

    原来如此

    app.component.ts

    
    declare var grecaptcha: any;
    const siteKey = 'YOUR_SITE_KEY';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.scss']
    })
    export class AppComponent implements OnInit {
     
    onSubmit() {
        grecaptcha.ready(() => {
          grecaptcha.execute(siteKey, { action: 'contactUs' }).then((token) => {
            console.log(token);
            let body = {
              firstName:this.firstName,
              captcha:token
            }
            this.recapService.checkRecaptcha(body).subscribe(res => {
              console.log('res', res);
            })
          });
        });
      }
    
    }
    

    服务

    import { HttpClient } from '@angular/common/http';
    import { Injectable } from '@angular/core';
    
    @Injectable({
      providedIn: 'root'
    })
    export class RecapService {
    
      constructor(private http: HttpClient) { }
    
      checkRecaptcha(body) {
        return this.http.post('http://localhost:3000/subscribe', body);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-16
      • 2023-01-04
      • 2019-05-04
      • 2019-11-08
      • 2021-02-16
      • 1970-01-01
      • 2019-05-14
      • 2021-07-07
      相关资源
      最近更新 更多