Overview
The@Component decorator marks a class as an Angular component and provides configuration metadata. Components are the most basic UI building blocks of an Angular application.
Signature
Parameters
Configuration object that specifies how the component should be processed, instantiated, and used at runtime.
Component Metadata
selector
CSS selector that identifies this component in a template. Can be an element name, class, attribute, or combination.
template / templateUrl
Inline HTML template for the component.
Path to an external HTML template file.
Use either
template or templateUrl, not both.styles / styleUrls / styleUrl
Inline CSS styles for the component.
Paths to external CSS stylesheet files.
Path to a single external CSS stylesheet file.
standalone
When
true, the component does not need to be declared in an NgModule. When false, it must be declared in an NgModule’s declarations array.imports
Standalone component dependencies - other components, directives, pipes, or NgModules that can be used in the template.
Only available for standalone components.
providers
Services or values available for dependency injection in this component and its children.
viewProviders
Services available only to the component’s view (not content children).
changeDetection
Strategy for detecting changes:
ChangeDetectionStrategy.Default- Check component on every change detection cycleChangeDetectionStrategy.OnPush- Check only when inputs change or events fire
encapsulation
Style encapsulation strategy:
ViewEncapsulation.Emulated- Emulate shadow DOM (default)ViewEncapsulation.None- No encapsulationViewEncapsulation.ShadowDom- Use native shadow DOM
inputs
Array of input property names or configuration objects.
outputs
Array of output property names.
exportAs
Name for template variable references.Usage:
<app-user-profile #profile="userProfile"></app-user-profile>host
Map of class properties to host element bindings.
hostDirectives
hostDirectives
Type<unknown>[] | {directive: Type<unknown>, inputs?: string[], outputs?: string[]}[]
Directives to apply to the host element.
Basic Example
user-profile.component.ts
Component with Inputs and Outputs
counter.component.ts
OnPush Change Detection
optimized.component.ts
With
OnPush, the component only checks for changes when:- An input reference changes
- An event originates from the component or its children
- Change detection is manually triggered
Component with External Files
app.component.ts
Related APIs
- @Directive - Create custom directives
- @Input - Define input properties
- @Output - Define output properties
- Lifecycle Hooks - React to component lifecycle events
See Also
Components Guide
Learn component fundamentals
Templates
Master template syntax
Styling
Style your components
Change Detection
Optimize performance
