3

I created a library using standalone components. In this library, I export related components in arrays like this:

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'". I also tried:

  • Using the spread operator to "flatten" the array
  • Exporting components as Provider[] or any[]
  • Exporting components without specifying the type of the array

I even tried using the same syntax as in the official Angular guide

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.

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. The issue seems to appear when I consume the library from a different application where I install it.

5
  • Not really but I do need to import other components as well. so it would be something like imports: [ MyComponent, TABLE_COMPONENTS, MyOtherComponent ]. I tried with the spread operator but it does not work. I don't get it as at the end, this is simply an array of types... Commented Jan 11, 2023 at 20:33
  • Not in the case of standalone components ;-) The imports I'm showing here is one of a component. Commented Jan 11, 2023 at 21:12
  • Ups! did not pay attention that you are dealing with standalone components. Commented Jan 11, 2023 at 21:16
  • Could not reproduce your problem. Is that right how I did it? stackblitz.com/edit/angular-ivy-mt3alb?file=src/app/… Commented Jan 11, 2023 at 21:19
  • I updated the OP to add an interesting point. Commented Jan 11, 2023 at 22:07

2 Answers 2

1

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. 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

Here's a working StackBlitz example showing it working as expected with no typing, and using the spread operator: StackBlitz Example

Sign up to request clarification or add additional context in comments.

2 Comments

I'll try that tomorrow but as said in the OP, I think I already did that and it didn't work. CHeck out the OP, I wrote an update.
I'm getting an error related to Angular being unable to statically analyze the array, 'imports' must be an array of components, directives, pipes, or NgModules. Value is of type '[ (not statically analyzable), (not statically analyzable), (not statically analyzable), RouterModule, SharedModule, ReactiveFormsModule, ButtonDirective, SearchFieldComponent, IconComponent]
0

What works for me was not making a 2-dimensional array, but directly put in each component "imports" the dependencies as array:

@Component({
......
imports:[TableComponent,TableHeaderComponent,TableBodyRowDirective,...etc]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.