typescript
36 lines · 8 steps
Deferred chart loading with @defer in Angular
A standalone dashboard component lazy-loads a heavy chart when it scrolls into view, with skeleton, loading, and error fallbacks.
Explained by
highlit
1import { Component, signal } from '@angular/core';
2import { CommonModule } from '@angular/common';
3import { RevenueChartComponent } from './revenue-chart.component';
4import { ReportService, RevenueReport } from './report.service';
5
6@Component({
7 selector: 'app-dashboard',
8 standalone: true,
9 imports: [CommonModule, RevenueChartComponent],
10 template: `
11 <section class="dashboard">
12 <header>
13 <h1>Quarterly Revenue</h1>
14 <button (click)="refresh()">Refresh</button>
15 </header>
16
17 @defer (on viewport; prefetch on idle) {
18 <app-revenue-chart [report]="report()" />
19 } @placeholder (minimum 300ms) {
20 <div class="chart-skeleton" aria-hidden="true"></div>
21 } @loading (after 150ms; minimum 400ms) {
22 <div class="chart-loading">Rendering chart…</div>
23 } @error {
24 <p class="chart-error">Unable to load the revenue chart.</p>
25 }
26 </section>
27 `,
28})
29export class DashboardComponent {
30 private readonly reports = inject(ReportService);
31 readonly report = signal<RevenueReport>(this.reports.snapshot());
32
33 refresh(): void {
34 this.reports.load().subscribe((data) => this.report.set(data));
35 }
36}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1@defer lets you defer a component's download and render until a real trigger fires, keeping the initial bundle lean.
- 2Placeholder, loading, and error blocks with timing hints prevent flicker and give users honest feedback during async work.
- 3Signals turn plain state into reactive template bindings that re-render automatically when set.
Related explainers
typescript
import { NestFactory } from '@nestjs/core'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import { ValidationPipe } from '@nestjs/common'; import { ApiProperty } from '@nestjs/swagger';
Wiring validation and Swagger docs in NestJS
validation
openapi
decorators
Intermediate
8 steps
python
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, status from pydantic import BaseModel, EmailStr from sqlalchemy.orm import Session
Building a signup endpoint in FastAPI
dependency-injection
request-validation
background-tasks
Intermediate
8 steps
typescript
import { Component, Input } from '@angular/core'; interface Order { id: string;
How Angular ICU plurals localize an order summary
i18n
pluralization
standalone-component
Intermediate
8 steps
php
<?php namespace App\Providers;
Subdomain multi-tenancy routing in Laravel
multi-tenancy
service-container
route-binding
Advanced
7 steps
typescript
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; import { Observable, catchError, concatMap, finalize } from 'rxjs'; import { DataSource, QueryRunner } from 'typeorm';
Wrapping requests in a transaction with NestJS
interceptors
transactions
rxjs
Advanced
7 steps
typescript
type CsvColumn<T> = { header: string; value: (row: T) => string | number | boolean | null | undefined; };
Building a type-safe CSV writer in TypeScript
generics
serialization
escaping
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/deferred-chart-loading-with-defer-in-angular-explained-typescript-1849/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.