基本上,当您使用来自 reCaptcha 验证程序的 .clear() 方法时,它会从页面清除 reCAPTCHA 小部件并销毁当前实例。这意味着当您在 dom 元素上初始化验证码并在运行 clear() 方法后,它也会删除验证码实例以及 dom 元素。您可以在下方查看更改前后的内容。在我们清除 recaptcha 之前,它正在删除 dom 元素,我们无法重新初始化,因为它不会获取该元素。此外,如果它已经在某个元素上初始化,则在清除之前无法重新初始化。
注册.component.html :前
<div id="captcha-element"></div>
注册.component.html :后
<div #captchaContainer>
<div id="captcha-element"></div>
</div>
注册.component.ts
declare var grecaptcha: any;
@Component({
selector: 'auth-signup',
templateUrl: './signup.component.html',
styleUrls: ['./signup.component.scss'],
})
export class LoginComponent implements OnDestroy {
otpIdentifier: string | null = null;
recaptchaVerifier: firebase.auth.RecaptchaVerifier | null = null;
recaptchaWidgetId: number | null = null;
@ViewChild('captchaContainer') captchaContainer!: ElementRef;
constructor() {}
async sendOtp() {
try {
if (!this.phoneNumber) {
return;
}
if (this.recaptchaVerifier && this.isSubmitting && !this.otpSent) {
//send otp here
}
} catch (error: any) {
console.error(error);
}
}
initiateRecaptchaContainer() {
this.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('captcha-element', {
'size': 'invisible',
'expired-callback': () => {
grecaptcha.reset(this.recaptchaWidgetId);
},
});
this.recaptchaVerifier?.render().then((id) => {
this.recaptchaWidgetId = id;
});
}
async resendOtp() {
this.clearRecaptcha();
this.initiateRecaptchaContainer();
if (this.recaptchaVerifier) {
//send otp here
}
}
clearRecaptcha() {
this.recaptchaVerifier?.clear();
this.captchaContainer.nativeElement.innerHTML = `<div id="captcha-element"></div>`;
}
returnAndReinitializeCaptcha() {
this.clearRecaptcha();
this.initiateRecaptchaContainer();
}
ngOnDestroy(): void {
}
ngAfterViewInit(): void {
this.initiateRecaptchaContainer();
}
}
下面是我们在组件中所做的更改
//to clear the captcha and adding the element to dom again so that we can reinitialize the captcha.
@ViewChild('captchaContainer') captchaContainer!: ElementRef;
clearRecaptcha() {
this.recaptchaVerifier?.clear();
this.captchaContainer.nativeElement.innerHTML = `<div id="captcha-element"></div>`;
}
我希望它也能解决你的问题。您可以在发现此类问题的任何类型的应用程序上使用相同的登录名。