New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exported array of standalone components cannot be imported #48089
Comments
For standalone components it may be beneficial to group multiple declarations into a single array, that can then be imported all at once in `Component.imports`. If this array is declared within a library, however, would the AOT compiler need to extract the contents of the array from the declaration file. This requires that the array is constructed using an `as const` cast, which results in a readonly tuple declaration in the generated .d.ts file of the library: ```ts export declare const DECLARATIONS: readonly [typeof StandaloneDir]; ``` The partial evaluator logic did not support this syntax, so this pattern was not functional when a library is involved. This commit adds the necessary logic in the static interpreter to evaluate this type at compile time. Closes angular#48089
NgModule should support readonly arrays in `@NgModule.imports` and `@NgModule.exports`. Fixes angular#48089
I have another problem with exported array of standalone components and directives... When i import to the module one by one:
both server and prod build works.
SERVER works, but prod builds is broken - somehow it brakes imports at all.. and it cannot even find things from other imported modules. Unfortunately.. i am not able to simulate it minimal repo :-/ Any idea what to try? What may be the cause? Directives are in Buildable library in NX Monorepo Probably this may be the issue: |
For standalone components it may be beneficial to group multiple declarations into a single array, that can then be imported all at once in `Component.imports`. If this array is declared within a library, however, would the AOT compiler need to extract the contents of the array from the declaration file. This requires that the array is constructed using an `as const` cast, which results in a readonly tuple declaration in the generated .d.ts file of the library: ```ts export declare const DECLARATIONS: readonly [typeof StandaloneDir]; ``` The partial evaluator logic did not support this syntax, so this pattern was not functional when a library is involved. This commit adds the necessary logic in the static interpreter to evaluate this type at compile time. Closes #48089 PR Close #48091
@AndrewKushnir maybe it solves this too |
@JoostK what do you think about closing ng-packagr/ng-packagr#2398? |
Thanks for the reminder, that was on the back of my mind :-) |
NgModule should support readonly arrays in `@NgModule.imports` and `@NgModule.exports`. Fixes angular#48089
@AndrewKushnir |
For standalone components it may be beneficial to group multiple declarations into a single array, that can then be imported all at once in `Component.imports`. If this array is declared within a library, however, would the AOT compiler need to extract the contents of the array from the declaration file. This requires that the array is constructed using an `as const` cast, which results in a readonly tuple declaration in the generated .d.ts file of the library: ```ts export declare const DECLARATIONS: readonly [typeof StandaloneDir]; ``` The partial evaluator logic did not support this syntax, so this pattern was not functional when a library is involved. This commit adds the necessary logic in the static interpreter to evaluate this type at compile time. Closes angular#48089 PR Close angular#48091
If I understood ur idea, U want to use components like this:
and avoid "Unable to import component ComponentNameHere." I solved it via:
|
@cvandradg No, the issue reported here is that a readonly array of standalone components can not be imported in |
Angular renaissance and momentum. |
@hakimio thank you, so the current suggested way of angular to export components, like this:
is currently not working, i got it right? (cuz It's not working for me unless I export them, I'm on Angular17) |
This is quite annoying, I'm refactoring our shared library for standalone components, just to find out the docs are lying :-( The PR is a year old |
Ah thnx...missed that one |
Still not resolved? :( |
Seems like the solution provided by @cvandradg is working. If I re-export shared components, then the error is not happening. Ex: import { MillifyPipe } from "./millify/millify.pipe";
export const SHARED_APP_COMPONENTS = [
MillifyPipe,
] as const;
export { MillifyPipe } from "./millify/millify.pipe"; |
It doesn't work with |
@hakimio for ng modules I think you can create a shared module to import instead. |
This issue is about libraries. |
exactly, it doesn't make sence to create an NgModule just to import multiple components. We want to get rit of the NgModules from the libraries, that is the whole point in the first place :-) |
No official workaround for this problem yet and docs are still the same in v17.3.2. I'm also porting libraries to the standalone approach, and I'm doing the following to chill out these errors: /*
* THIS IS MY LIBRARY public-api.ts FILE
* Public API Surface of A Library
*/
export {AComponent} from './lib/a/a.component';
import {AComponent} from './lib/a/a.component';
export const NGX_A_LIBRARY_COMPONENTS = [AComponent] as const; // <-- This is NG docs recommendation
/*
* THIS BELONGS TO THE HOST APP USING THE LIBRARY
* App Module
*/
import {NGX_A_LIBRARY_COMPONENTS, AComponent} from '@company/a-library';
@NgModule({
declarations: [...],
imports: [
...
NGX_A_LIBRARY_COMPONENTS as [typeof AComponent], // <-- This might do the trick
]
})
export class AppModule {
} Perhaps not ideal but worth the try. |
Which @angular/* package(s) are the source of the bug?
Don't known / other
Is this a regression?
No
Description
I have a library that export multiple component in a
const
as advised in the standalone guide:Whenever I import this constant from a module or in a component in an application that use my library, I have the following issue:
If I redefined the same const in the application, it works.
Links
Please provide a link to a minimal reproduction of the bug
https://stackblitz.com/edit/angular-ivy-5rgxzc?file=src%2Fapp%2Fapp.module.ts
Please provide the exception or error you saw
Please provide the environment you discovered this bug in (run
ng version
)Anything else?
Love Standalone components!
The text was updated successfully, but these errors were encountered: