Performance: Component decorator and metadata
This affects the initial page load speed and rendering performance by controlling how Angular compiles and renders components.
import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: `<h1>Welcome</h1>`, styles: [`h1 { color: darkblue; }`] }) export class AppComponent {}
import { Component } from '@angular/core'; import { trigger, transition, style, animate } from '@angular/animations'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], animations: [ trigger('fade', [ transition(':enter', [ style({ opacity: 0 }), animate('500ms ease-in', style({ opacity: 1 })) ]) ]) ] }) export class AppComponent {}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| External template and styles with animations | Moderate DOM nodes | Multiple reflows due to animations | High paint cost | [X] Bad |
| Inline template and styles without animations | Minimal DOM nodes | Single reflow | Low paint cost | [OK] Good |
@Component decorator in Angular?@Component specifically does@Component marks a class as a component and provides metadata like selector and template.template is for inline HTML, templateUrl is for external file path.template: 'Hello
' is valid for inline HTML.@Component({ selector: 'app-hello', template: '{{ message }}
' })
export class HelloComponent { message = 'Hi!'; }<app-hello></app-hello> is used?@Component({
selector: 'app-test',
templateUrl: './test.component.html',
styles: ['./test.component.css']
})
export class TestComponent {}styles is for inline CSS strings; styleUrls is for external CSS files.styles with a file path array, which is incorrect; it should be styleUrls.app-button that uses an external HTML template and multiple CSS files. Which is the correct component decorator configuration?templateUrl with the path to the HTML file.styleUrls with an array of CSS file paths.templateUrl: './button.html' and styleUrls: ['./btn.css', './colors.css'] is correct. Others incorrectly use template for a file path or styles for external CSS files.Pass the test and this pattern moves unknown