-
Notifications
You must be signed in to change notification settings - Fork 26.5k
Description
Which @angular/* package(s) are relevant/related to the feature request?
core
Description
With the increasing adoption of standalone components and signals, traditional hooks are becoming less essential. However, there are still cases where we need to perform specific setup tasks before a component is fully initialized.
For example, in our project, we use an svgIconRegistry that requires registering icons by name and path before they can be used in a template. This logic cannot be handled within a router resolver, as it may depend on component inputs.
A solution could be introducing { resolve: ResolveComponentFn } in the component declaration, allowing developers to define preloading logic directly within the component context. This approach would improve modularity and offer a cleaner way to handle component-specific initialization.
I believe many other use cases exist where a Component Resolver would be highly beneficial.
Proposed solution
Introduce a Component Resolver mechanism that allows defining component-specific initialization logic within the component declaration. This would work similarly to router resolvers but at the component level, ensuring that necessary data or setup tasks are completed before the component is rendered.
Suggested API
Extend the component metadata with a new resolve property:
@Component({
selector: 'app-example',
standalone: true,
templateUrl: './example.component.html',
resolve: resolveExampleComponent
})
export class ExampleComponent {
constructor(private svgIconRegistry: SvgIconRegistry) {}
}
Where resolveExampleComponent is a function that executes before the component initializes:
const resolveExampleComponent: ResolveComponentFn = async (route, componentInputs) => {
await svgIconRegistry.register('icon-name', 'assets/icons/icon.svg');
};
Key Benefits
Encapsulation – Keeps component-specific logic within the component itself.
Flexibility – Allows preloading data or executing setup logic that depends on component inputs.
Improved Maintainability – Reduces reliance on global router resolvers or complex workarounds.
This feature would significantly enhance how we manage component lifecycle and dependencies in Angular applications.
Alternatives considered
Alternatives Considered
Using a Router Resolver
Why not? Router resolvers execute before navigation completes, but they are not component-aware. Since some initialization logic depends on component inputs, this approach is not viable in many cases.
Handling Initialization in ngOnInit or Constructor
Why not? This can lead to inconsistent behavior, as asynchronous initialization might cause flickering or require additional loading states within the component.
Creating a Wrapper Component
Why not? Wrapping components in a higher-order component for preloading logic introduces extra complexity and makes the code less maintainable.
Using Signals or Effects for Initialization
Why not? Signals and effects help with reactivity but do not guarantee that setup logic runs before the component is rendered.
Why a Component Resolver?
A Component Resolver provides a clean, declarative way to manage initialization without affecting routing, global state, or component structure. It ensures that component-specific logic runs before rendering, making it an optimal solution for scenarios like SVG icon registration, data fetching, and other component dependencies.

