Performance: Standalone component declaration
This affects the initial page load speed and bundle size by reducing module overhead and simplifying dependency resolution.
Jump into concepts and practice - no test required
import { Component } from '@angular/core'; @Component({ selector: 'app-example', standalone: true, template: `<p>Hello World</p>` }) export class ExampleComponent {}
import { Component, NgModule } from '@angular/core'; @Component({ selector: 'app-example', template: `<p>Hello World</p>` }) export class ExampleComponent {} @NgModule({ declarations: [ExampleComponent], imports: [], bootstrap: [ExampleComponent] }) export class AppModule {}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| NgModule-based component | Standard DOM nodes | 1 reflow per component bootstrap | Normal paint cost | [X] Bad |
| Standalone component | Standard DOM nodes | 1 reflow per component bootstrap | Normal paint cost | [✓] Good |
standalone: true in an Angular component's decorator do?standalone: truestandalone: true allows the component to be used without declaring it in any NgModule.template or external with templateUrl. The class must be exported properly.import { Component } from '@angular/core';
@Component({
selector: 'app-hello',
standalone: true,
template: `Hello, {{ name }}!
`
})
export class HelloComponent {
name = 'Angular';
}name = 'Angular', so interpolation outputs 'Angular'.@Component({
selector: 'app-sample',
standalone: true,
template: 'Sample
',
imports: [CommonModule]
})
export class SampleComponent {}ButtonComponent. How should you declare the imports array in your component decorator?