Components: one class, one template, scoped styles
Components are the unit of thought and test
A component is a class plus its template plus its scoped CSS. The class holds state and behavior; the template is a projection of that state; the CSS is scoped to this component by default. Inputs (@Input() or the new input()) are the component's public API; outputs (@Output() emitters) are how it talks back.
@Component({
standalone: true,
selector: 'app-invoice-card',
template: `
<article class="card">
<h3>{{ invoice.title }}</h3>
<button (click)="pay.emit(invoice.id)">Pay</button>
</article>
`,
})
export class InvoiceCardComponent {
@Input({ required: true }) invoice!: Invoice;
@Output() pay = new EventEmitter<number>();
}Takeaways
- Pass data down with inputs; bubble decisions up with outputs.
- Keep templates declarative; push logic into the class or pure pipes.
- Prefer
standalone: true— modules are legacy for new code.
Enjoying This Lesson?
Your support helps create more comprehensive courses and lessons like this one. Help me build better learning experiences for everyone.
Support Awashyak