如何在Vue组件中添加grecaptcha onload方法。未加载时grecaptcha.render不是函数
const script = document.createElement("script");
script.src = `${URL}?render=explicit&hl=TR`;
script.async = true;
script.defer = true;
document.body.appendChild(script);
this.initReCaptcha();
initReCaptcha: function () {
setTimeout(function () {
if (typeof grecaptcha === 'undefined') {
this.initReCaptcha();
} else {
grecaptcha.render('recaptcha', {
sitekey: 'XXXX',
callback: this.submit,
'expired-callback': this.expired
});
}
}.bind(this), 100);
}
Run Code Online (Sandbox Code Playgroud)
我将超时设置为1000时正在工作,但为时已晚。