如何将 angular 组件[的选择器] 键入变量?

How to type an angular component['s selector] to a variable?

我正在将自定义多拖放功能构建到 angular 应用程序中,并且在其中一项服务中,我遍历了一系列主机组件 (<app-cell>)。

当我遍历 的数组时,有没有办法将 "custom typing" 分配给我的迭代变量?换句话说,如何将 tile 的类型限制为 app-tile?

这不是确切的代码,但或多或​​少地说明了我正在努力实现的目标。

let selectedTiles = Array.from(document.querySelectorAll('app-tile'));

selectedTiles.map((tile: **WHAT GOES HERE?**) => {
  // do something with tile
}

我以前使用过模型 'custom typing' 具有特定键的简单对象,但我不知道如何尝试缩小对我自己的 angular 组件的分配范围。

根据我自己的谷歌搜索,TemplateRef 或指令似乎在正确的路径上,但我对其中任何一个的理解都是模糊的,并且可以使用一些方向来确定我可以使用的类型!谢谢!

这真的取决于“selectedTiles”是什么类型。 它的类型类似于 Array<T>,假设这里是某种类型 { id: number, dataset: Array<string> }。 由于您在 selectedTile 数组上进行映射,因此每个元素(此处为“tile”)的类型为 T.

更具体地说,您能否分享一下您正在使用的服务。另外,如果可能的话,您可以分享代码 link

在阅读 angular 文档时偶然发现了答案。不幸的是,自定义输入是不可能的:

When [Generic DOM APIs, such as document.createElement() or document.querySelector() are] called with unknown elements, such as a custom element name (popup-element in our example), the methods will return a generic type, such as HTMLElement, since TypeScript can't infer the correct type of the returned element.

Link here