简体   繁体   English

在 Angular 独立组件中将组件作为数组导入

[英]Import components as array in Angular standalone components

I created a library using standalone components.我使用独立组件创建了一个库。 In this library, I export related components in arrays like this:在这个库中,我像这样导出 arrays 中的相关组件:

export const TABLE_COMPONENTS: Type<any>[] = [
  TableComponent,
  TableHeaderComponent,
  TableBodyRowDirective,
  TableHeaderRowDirective,
  TableBodyEmptyRowDirective,
  TableFooterRowDirective,
  TableFirstRowDirective,
  TableFilterIconDirective,
  TableLastRowDirective
];

But when I try to use this array in the consuming application it does not work.但是当我尝试在消费应用程序中使用这个数组时它不起作用。 I tried like this:我试过这样的:

imports: [
    TABLE_COMPONENTS
],

But I get the error "'imports' must be an array of components, directives, pipes, or NgModules. Value is a reference to 'TABLE_COMPONENTS'".但我收到错误“'imports' 必须是组件、指令、管道或 NgModules 的数组。值是对 'TABLE_COMPONENTS' 的引用”。 I also tried:我也试过:

  • Using the spread operator to "flatten" the array使用扩展运算符“展平”数组
  • Exporting components as Provider[] or any[]将组件导出为Provider[]any[]
  • Exporting components without specifying the type of the array在不指定数组类型的情况下导出组件

I even tried using the same syntax as in the official Angular guide我什至尝试使用与官方 Angular 指南中相同的语法

export const TABLE_COMPONENTS = [
  TableComponent,
  TableHeaderComponent,
  TableBodyRowDirective,
  TableHeaderRowDirective,
  TableBodyEmptyRowDirective,
  TableFooterRowDirective,
  TableFirstRowDirective,
  TableFilterIconDirective,
  TableLastRowDirective
] as const;

The weird thing with this syntax is that vscode does not complain, but then, I get the error above during the compilation.这种语法的奇怪之处在于 vscode 没有报错,但是,我在编译过程中得到了上面的错误。

Does anyone know how to export related components into an array in order to import them at once instead of one by one?有谁知道如何将相关组件导出到数组中以便一次导入它们而不是一个一个地导入?

Updated:更新:

One important thing I forgot to mention is that I have a playground application for this library that references it directly (so not via a npm link or by installing the package) and there, it works.我忘记提及的一件重要事情是,我有一个直接引用它的库的 playground 应用程序(所以不是通过 npm 链接或通过安装包)并且在那里,它可以工作。 The issue seems to appear when I consume the library from a different application where I install it.当我从安装它的其他应用程序使用该库时,似乎会出现此问题。

The problem is that you're making a 2-dimensional array the way you have it set up.问题是您正在按照设置的方式制作二维数组。

export const TABLE_COMPONENTS: Type<any>[] = [
  TableComponent,
  TableHeaderComponent,
  TableBodyRowDirective,
  TableHeaderRowDirective,
  TableBodyEmptyRowDirective,
  TableFooterRowDirective,
  TableFirstRowDirective,
  TableFilterIconDirective,
  TableLastRowDirective
];


imports: [
    TABLE_COMPONENTS
],

After this, your imports will be an array with 1 element, which is itself an array.在此之后,您的imports将是一个包含 1 个元素的数组,它本身就是一个数组。 If you use the spread operator, it should work as you intend:如果您使用传播运算符,它应该按您的预期工作:

imports: [
    ...TABLE_COMPONENTS
]

You may also need to remove the Type<any>[] typing from the TABLE_COMPONENTS to coerce it您可能还需要从TABLE_COMPONENTS中删除Type<any>[]类型以强制它

Here's a working StackBlitz example showing it working as expected with no typing, and using the spread operator: StackBlitz Example这是一个工作的 StackBlitz 示例,显示它按预期工作,无需键入,并使用展开运算符: StackBlitz Example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM