How does a standalone component differ from a regular one?
A standalone component in Angular is a component that is not tied to a module (NgModule) and can work on its own.
Main differences from a regular component
- Does not require NgModule - regular components must always be declared in a module's
declarationsarray, standalone components do not. - Direct dependency import - the
importsfield of the@Componentdecorator can list other components, directives, and pipes needed for it to work. - Simplified loading - standalone components are easy to use with lazy loading or to embed in other standalone components without creating a separate module.
- Compatibility - a regular component can still be used inside a standalone component via
imports.
Example of declaring a standalone component:
typescript
@Component({
selector: 'app-standalone',
standalone: true,
template: `<p>I am a standalone component!</p>`
})
export class StandaloneComponent {}Conclusion: standalone components simplify the application's structure, reduce dependency on modules, and make reuse easier.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.