IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    新手入门Nest.js(十三)- Providers - Services

    Durban发表于 2020-10-14 22:07:35
    love 0

    Providers是什么,看下官网的解释

    Providers are a fundamental concept in Nest. Many of the basic Nest classes may be treated as a provider – services, repositories, factories, helpers, and so on. The main idea of a provider is that it can inject dependencies; this means objects can create various relationships with each other, and the function of "wiring up" instances of objects can largely be delegated to the Nest runtime system. A provider is simply a class annotated with an @Injectable() decorator.

    Services

    通过命令行创建 Services

    nest g service cats

    执行后结果类似如下

    $ nest g service cats
    CREATE src/cats/cats.service.spec.ts (446 bytes)
    CREATE src/cats/cats.service.ts (88 bytes)
    UPDATE src/app.module.ts (544 bytes)

    创建完src/cats/cats.service.ts代码如下

    import { Injectable } from '@nestjs/common';
    
    @Injectable()
    export class CatsService {}

    修改下,修改前提我们添加一个interface

    如果创建interface,命令行命令如下

    $ nest g interface cat
    CREATE src/cat.interface.ts (24 bytes)

    创建完src/cat.interface.ts代码如下

    export interface Cat {}

    修改后src/cats/cats.service.ts的代码如下

    import { Injectable } from '@nestjs/common';
    import { Cat } from 'src/cat.interface';
    
    @Injectable()
    export class CatsService {
      private readonly cats: Cat[] = [];
    
      create(cat: Cat) {
        this.cats.push(cat);
      }
    
      findAll() {
        return this.cats;
      }
    }

    顺便修改src/cat.interface.ts,代码如下

    export interface Cat {
      name: string;
      age: number;
      bread: string;
    }

    下面看下如何在Controller中应用

    constructor(private catsService: CatsService) {}
    
    @Get()
    async findAll(@Req() request: Request): Promise<Cat[]> {
      return this.catsService.findAll();
    }
    
    @Post()
    async create(@Body() createCatDto: CreateCatDto) {
      this.catsService.create(createCatDto);
      console.log(createCatDto);
      return 'This action will create a new cat';
    }

    上面代码是主要关注的地方

    另外需要导入对应的包

    import { Cat } from 'src/cat.interface';
    import { CatsService } from './cats.service';

    然后访问http://127.0.0.1:3000/cats会得到一个空数组

    执行下面的命令

    $ curl -d 'name=durban1&age=12&bread=1' http://127.0.0.1:3000/cats

    然后在访问http://127.0.0.1:3000/cats类似如下的数据

    [{"name":"durban1","age":"12","bread":"1"},{"name":"durban1","age":"12","bread":"1"}]

     



沪ICP备19023445号-2号
友情链接