业务逻辑放在 Service 中,由容器自动注入。

本节要点

  • @Injectable 声明可注入
  • 构造函数注入依赖
  • 控制器只处理请求与响应

代码示例

import { Injectable } from "@nestjs/common"

@Injectable()
export class UsersService {
  private users = [{ id: 1, name: "Alice" }]

  findAll() {
    return this.users
  }
}