为什么 Angular 中的 HTMLDialogElement 没有 showModal() 方法?

Why the HTMLDialogElement in Angular doesn't have showModal() method?

当我写这篇文章时,MDN 显示 HTMLDialogElement 在除 Internet Explorer 之外的所有浏览器中都受支持。 但奇怪的是,在使用它时,有一个警告说它在大多数浏览器中不受支持并标记为已弃用。那不是问题,直到我发现调用 showModal() 给我错误:

属性 'showModal' 在类型 HTMLDialogElement

上不存在

我是不是漏掉了什么?

这是我的代码:

let elem: HTMLDialogElement = document.getElementById("dlg") as HTMLDialogElement;
elem.showModal(); // this line gives error

根据 HTMLDialogElement 的类型定义 (lib.dom.d.ts),没有方法 showModal()。您可以将 elem 转换为 any 以使 TypeScript Transpiler 接受它:

(elem as any).showModal()

但是,您不应使用已弃用的 API。如果您将 Material 与 Angular 一起使用,则可以改用 MatDialog 服务。