簡體   English   中英

如何在 Angular 中使用 recaptcha v3?

[英]How can i use recaptcha v3 in Angular?

我知道那里有一些 ng 驗證碼庫。 我不想只通過一種方法調用來安裝 npm 依賴項。 在純 java 腳本實現中,例如 onSubmit 方法調用,當我們提交表單時,我們從 google recaptcha api 調用此方法

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

但我找不到辦法,我怎么能告訴 angular 和 typescript 這個 recaptcha 變量來自谷歌 api ,它將執行execute方法。

現在我當然得到了錯誤

 recaptcha.execute is not a function

somoene 找到了沒有圖書館的方法嗎?

所以這就是方式

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);
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM