首页 > 解决方案 > 如何在 TypeScript 中设置 ariaLabel 值

问题描述

我有一个打字稿反应应用程序,我正在尝试动态设置 ariaLabel 值,ESLint 正在抱怨Property 'ariaLabel' does not exist on type 'HTMLButtonElement'

我尝试了多种类型,但没有一个具有 ariaLabel 属性,我不想使用<any>

const ref = useRef<HTMLButtonElement>(null);
const buttonRef = ref.current.querySelector<HTMLButtonElement>(`[aria-label="${buttonAriaLabel}"]`);
buttonRef.ariaLabel = eventLabel; // here is the error

标签: javascriptreactjstypescript

解决方案


aria-label 是一个属性,因此您可以更改代码

buttonRef.setAttribute("aria-label", eventLabel);

推荐阅读