关于 d2-02-api-service
用于为 up-api CRUD 端点公开的实体生成 Angular 服务,并使用 HttpClient。若对话尚未确定实体,会先询问;此步骤无需重新部署。
主要功能
- 01Angular 服务模板
- 02按实体创建 CRUD 服务
- 03HttpClient 配置说明
适用场景
- 01为 up-api 实体创建服务
- 02在 app.config.ts 注册 provideHttpClient
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ softinter-chiangrai/ up-ai-skill-training --skill d2-02-api-service安装后在 Claude Code 中输入/d2-02-api-service确认已加载。
详细介绍
第 2 天 · 第 2 步 — API 服务
如果此对话中尚未确定实体,请询问要使用哪个实体(必须与 up-api 的 CRUD 端点所公开的实体一致)。
up-app/src/app/services/<entity>.service.ts:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
export interface <Entity> {
id?: number;
// one field per column from the Day 1 entity spec
}
@Injectable({ providedIn: 'root' })
export class <Entity>Service {
private base = 'http://localhost:8080/api/<entity>s';
constructor(private http: HttpClient) {}
getAll(): Observable<<Entity>[]> { return this.http.get<<Entity>[]>(this.base); }
getById(id: number): Observable<<Entity>> { return this.http.get<<Entity>>(`${this.base}/detail/${id}`); }
save(item: <Entity>): Observable<<Entity>> { return this.http.post<<Entity>>(`${this.base}/save`, item); }
update(id: number, item: <Entity>): Observable<<Entity>> { return this.http.put<<Entity>>(`${this.base}/update/${id}`, item); }
delete(id: number): Observable<void> { return this.http.delete<void>(`${this.base}/${id}`); }
}
如果 app.config.ts 中尚未注册 provideHttpClient(),请进行注册。此步骤无需重新部署。
评论
支持 Markdown