angular-performance
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseNgOptimizedImage (REQUIRED for images)
NgOptimizedImage(图片优化必备)
typescript
import { NgOptimizedImage } from '@angular/common';
@Component({
imports: [NgOptimizedImage],
template: `
<!-- LCP image: add priority -->
<img ngSrc="hero.jpg" width="800" height="400" priority>
<!-- Regular: lazy loaded by default -->
<img ngSrc="thumb.jpg" width="200" height="200">
<!-- Fill mode (parent needs position: relative) -->
<img ngSrc="bg.jpg" fill>
<!-- With placeholder -->
<img ngSrc="photo.jpg" width="400" height="300" placeholder>
`
})typescript
import { NgOptimizedImage } from '@angular/common';
@Component({
imports: [NgOptimizedImage],
template: `
<!-- LCP image: add priority -->
<img ngSrc="hero.jpg" width="800" height="400" priority>
<!-- Regular: lazy loaded by default -->
<img ngSrc="thumb.jpg" width="200" height="200">
<!-- Fill mode (parent needs position: relative) -->
<img ngSrc="bg.jpg" fill>
<!-- With placeholder -->
<img ngSrc="photo.jpg" width="400" height="300" placeholder>
`
})Rules
规则
- ALWAYS set and
width(orheight)fill - Add to LCP (Largest Contentful Paint) image
priority - Use not
ngSrcsrc - Parent of image must have
fillposition: relative/fixed/absolute
- 必须设置和
width(或height)fill - 为LCP(最大内容绘制)图片添加属性
priority - 使用而非
ngSrcsrc - 图片的父元素必须设置
fillposition: relative/fixed/absolute
@defer - Lazy Components
@defer - 懒加载组件
html
@defer (on viewport) {
<heavy-component />
} @placeholder {
<p>Placeholder shown immediately</p>
} @loading (minimum 200ms) {
<spinner />
} @error {
<p>Failed to load</p>
}html
@defer (on viewport) {
<heavy-component />
} @placeholder {
<p>Placeholder shown immediately</p>
} @loading (minimum 200ms) {
<spinner />
} @error {
<p>Failed to load</p>
}Triggers
触发条件
| Trigger | When to Use |
|---|---|
| Below the fold content |
| Load on click/focus/hover |
| Load when browser is idle |
| Load after delay |
| Load when expression is true |
html
<!-- Multiple triggers -->
@defer (on viewport; on interaction) {
<comments />
}
<!-- Conditional -->
@defer (when showComments()) {
<comments />
}| 触发方式 | 使用场景 |
|---|---|
| 视口外(折叠下方)的内容 |
| 点击/获取焦点/悬停时加载 |
| 浏览器空闲时加载 |
| 延迟指定时间后加载 |
| 当表达式为真时加载 |
html
<!-- Multiple triggers -->
@defer (on viewport; on interaction) {
<comments />
}
<!-- Conditional -->
@defer (when showComments()) {
<comments />
}Lazy Routes
懒加载路由
typescript
// Single component
{
path: 'admin',
loadComponent: () => import('./features/admin/admin').then(c => c.AdminComponent)
}
// Feature with child routes
{
path: 'users',
loadChildren: () => import('./features/users/routes').then(m => m.USERS_ROUTES)
}typescript
// Single component
{
path: 'admin',
loadComponent: () => import('./features/admin/admin').then(c => c.AdminComponent)
}
// Feature with child routes
{
path: 'users',
loadChildren: () => import('./features/users/routes').then(m => m.USERS_ROUTES)
}SSR & Hydration
SSR与水合作用
typescript
bootstrapApplication(AppComponent, {
providers: [
provideClientHydration()
]
});| Scenario | Use |
|---|---|
| SEO critical (blog, e-commerce) | SSR |
| Dashboard/Admin | CSR |
| Static marketing site | SSG/Prerender |
typescript
bootstrapApplication(AppComponent, {
providers: [
provideClientHydration()
]
});| 场景 | 推荐方案 |
|---|---|
| SEO关键场景(博客、电商) | SSR |
| 仪表盘/后台管理系统 | CSR |
| 静态营销网站 | SSG/预渲染 |
Slow Computations
缓慢计算处理方案
| Solution | When |
|---|---|
| Optimize algorithm | First choice always |
| Pure pipes | Cache single result |
| Memoization | Cache multiple results |
| Derived signal state |
NEVER trigger reflows/repaints in lifecycle hooks (, ).
ngOnInitngAfterViewInit| 解决方案 | 适用场景 |
|---|---|
| 优化算法 | 首选方案 |
| 纯管道(Pure pipes) | 缓存单一结果 |
| 记忆化(Memoization) | 缓存多个结果 |
| 派生信号状态 |
切勿在生命周期钩子(、)中触发重排/重绘。
ngOnInitngAfterViewInit