angular
Use when building, refactoring, or debugging Angular (v20/21+): standalone components, signals, zoneless change detection, @if/@for/@defer control flow, inject() DI, resource()/httpResource(), RxJS interop, NgRx SignalStore, ng CLI. NOT React (that is react), NOT Next.js (that is nextjs), NOT a TypeScript language question (that is typescript).
ericrisco · 已有 AI 审核记录
安装与使用
运行命令前先阅读来源与依赖说明。复制命令不会自动安装或执行。
npx skills add ericrisco/rsc-harness --skill angular技能说明
name: angular description: "Use when building, refactoring, or debugging Angular (v20/21+): standalone components, signals, zoneless change detection, @if/@for/@defer control flow, inject() DI, resource()/httpResource(), RxJS interop, NgRx SignalStore, ng CLI. NOT React (that is react), NOT Next.js (that is nextjs), NOT a TypeScript language question (that is typescript)." tags: [angular, frontend, web, signals, typescript, spa] recommends: [typescript, testing-web, secure-coding] origin: risco
Angular — Standalone, Signals, Zoneless (Angular 20/21+)
Build Angular the way it ships in 2026: standalone components, signals as the reactivity model, zoneless change detection, built-in control flow, and
inject()DI. Treat NgModules,*ngFor, and@Input()decorators as legacy you only touch to migrate.
Not this skill
AngularJS (1.x) is out of scope entirely — this skill is Angular 2+ only and the APIs do not map.
Route elsewhere for React → ../react/SKILL.md; Next.js App Router → ../nextjs/SKILL.md;
Vue/Nuxt, Svelte, SolidJS, Astro → ../vue-nuxt/SKILL.md, ../svelte/SKILL.md,
../solid-js/SKILL.md, ../astro/SKILL.md; a pure TypeScript language question (generics,
narrowing, tsconfig) with no Angular dimension → ../typescript/SKILL.md; a standalone NestJS
API → ../nestjs/SKILL.md; a generic Node service → ../nodejs/SKILL.md; cross-framework
Playwright e2e strategy → ../testing-web/SKILL.md / ../e2e-testing/SKILL.md. Angular Universal
SSR and Angular's own ng test (Vitest) setup stay here.
Decide first
| Situation | Do this | Why |
|---|---|---|
| Greenfield app / new feature | Zoneless + signals + standalone by default. ng new (Angular 21) already excludes Zone.js. | The defaults shipped stable in v20-v21; fight them and you write more code that the framework now does for you. |
| Brownfield NgModule + decorator app | Migrate incrementally with the schematics in references/migration.md (NgModule→standalone, control flow, decorator→signal inputs, Zone.js→zoneless, Karma→Vitest); do not rewrite. Keep Zone.js until you flip it on purpose. | A working app that uses *ngIf is not a bug. Churn introduces risk for no user value. |
| "View not updating" complaint | Jump to the change-detection section: signal not read in template, OnPush without a signal, or stale Zone.js assumption. | Zoneless means a mutation that no signal observes will never repaint — the fix is structural, not a detectChanges() call. |
The modern baseline
No NgModules. Bootstrap a standalone root component and configure providers in app.config.ts.
// main.ts
import { bootstrapApplication } from '@angular/platform-browser';
import { App } from './app/app';
import { appConfig } from './app/app.config';
bootstrapApplication(App, appConfig);
// app/app.config.ts
import { ApplicationConfig, provideZonelessChangeDetection } from '@angular/core';
import { provideRouter } from '@angular/router';
import { provideHttpClient, withFetch } from '@angular/common/http';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [
provideZonelessChangeDetection(), // no Zone.js; CD driven by signals + events
provideRouter(routes),
provideHttpClient(withFetch()),
],
};
- Rule: one
bootstrapApplicationcall, providers inapp.config.ts. Why: NgModule bootstrap (platformBrowserDynamic().bootstrapModule(AppModule)) is the legacy path — more files, slower to reason about. - Rule: components are
standaloneby default (thestandaloneflag is implied in v20+; do not writestandalone: truein new code, and never writestandalone: false). Why: standalone is the framework default now; the flag is noise.
Bad → Good
// Bad — NgModule wiring for a single component
@NgModule({ declarations: [UserCard], imports: [CommonModule], exports: [UserCard] })
export class UserCardModule {}
// Good — standalone component imports only what it uses
@Component({
selector: 'app-user-card',
imports: [DatePipe],
template: `<p>{{ joined() | date }}</p>`,
})
export class UserCard {
joined = input.required<Date>();
}
Signals as the reactivity model
signal() holds state, computed() derives it, effect() runs side effects, linkedSignal() resets writable state when a source changes.
import { signal, computed, effect, linkedSignal } from '@angular/core';
const qty = signal(1);
const price = signal(9.99);
const total = computed(() => qty() * price()); // derived — recomputes lazily
const draftQty = linkedSignal(() => qty()); // writable, resets when qty changes
effect(() => console.log('total changed:', total())); // side effect ONLY (logging, DOM, sync)
- Rule: derive with
computed(), never witheffect(). Why: aneffect()that writes a signal to "compute" a value creates a hidden dependency graph that loops or fires extra times —computed()is pull-based and memoized. - Rule:
effect()is for side effects (logging,localStorage, imperative DOM, 3rd-party libs), not for keeping two signals in sync. Why: synced state belongs incomputed()orlinkedSignal().
Component I/O is signal-based: input(), input.required(), output(), model() for two-way.
Bad → Good
// Bad — decorator I/O, mutable, no type-safety on required
@Input() userId!: string;
@Output() saved = new EventEmitter<User>();
// Good — signal inputs/outputs
userId = input.required<string>(); // read as userId()
saved = output<User>(); // emit with saved.emit(user)
name = model(''); // two-way: [(name)]="..."
Templates: built-in control flow
Use @if / @for / @switch / @defer. The legacy *ngIf / *ngFor / *ngSwitch structural directives are deprecated.
@if (user(); as u) {
<h1>{{ u.name }}</h1>
} @else {
阅读完整来源 ↗