我知道有一些 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 找到了一种无需库的方法吗?
最佳答案
所以这就是方法
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);
}
}
关于angular - 如何在 Angular 中使用 recaptcha v3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65826766/