我正在将Recaptcha添加到我的角度应用程序中。我引用了this tutorial,其中包括以下用于呈现Recaptcha的内容:
@ViewChild('recaptcha') recaptchaElement: ElementRef;
...
renderReCaptcha() {
window['grecaptcha'].render(this.recaptchaElement.nativeElement, {
'sitekey' : 'MY SITE KEY',
'callback': (response) => {
console.log(response);
}
});
}
一切正常,但我不确定这是否不好。 nativeElement文档指示以下内容:
当需要直接访问DOM时,将此API作为最后的手段。 改用Angular提供的模板和数据绑定。 另外,您可以看看Renderer2,它提供了 即使直接访问本机元素也可以安全使用的API 不支持。
recaptcha元素是动态创建的,我在模板中没有直接访问它的权限。在上下文中使用Renderer2是更好的选择吗?如果是的话,相当于window.render?