Skip to content

Commit

Permalink
Merge pull request ascoders#320 from rmlzy/patch-1
Browse files Browse the repository at this point in the history
Update 20.精读《Nestjs》文档.md
  • Loading branch information
ascoders authored May 31, 2021
2 parents 3e299d8 + 0a090cf commit d81e6d7
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions 前沿技术/20.精读《Nestjs》文档.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ Nestjs 是我见过的,将 Typescript 与 Nodejs Framework 结合的最好的

Nestjs 不是一个新轮子,它是基于 Express、socket.io 封装的 nodejs 后端开发框架,对 Typescript 开发者提供类型支持,也能优雅降级供 Js 使用,拥有诸多特性,像中间件等就不展开了,本文重点列举其亮点特性。

## 2.1 Modules, Controllers, Components
## 2.1 Modules, Controllers, Providers

Nestjs 开发围绕着这三个单词,Modules 是最大粒度的拆分,表示应用或者模块。Controllers 是传统意义的控制器,一个 Module 拥有多个 Controller。Components 一般用于做 Services,比如将数据库 CRUD 封装在 Services 中,每个 Service 就是一个 Component
Nestjs 开发围绕着这三个单词,Modules 是最大粒度的拆分,表示应用或者模块。Controllers 是传统意义的控制器,一个 Module 拥有多个 Controller。Providers 一般用于做 Services,比如将数据库 CRUD 封装在 Services 中,每个 Service 就是一个 Provider

## 2.2 装饰器路由

Expand Down Expand Up @@ -50,7 +50,7 @@ export class UsersController {

## 2.3 模块间依赖注入

Modules, Controllers, Components 之间通过依赖注入相互关联,它们通过同名的 `@Module` `@Controller` `@Component` 装饰器申明,如:
Modules, Controllers, Providers 之间通过依赖注入相互关联,它们通过同名的 `@Module` `@Controller` `@Injectable` 装饰器申明,如:

```typescript
@Controller()
Expand All @@ -61,7 +61,7 @@ export class UsersController {
```

```typescript
@Component()
@Injectable()
export class UsersService {
getAllUsers() {
return []
Expand All @@ -72,12 +72,12 @@ export class UsersService {
```typescript
@Module({
controllers: [ UsersController ],
components: [ UsersService ],
providers: [ UsersService ],
})
export class ApplicationModule {}
```

`ApplicationModule` 申明其内部 Controllers 与 Components 后,就可以在 Controllers 中注入 Components 了:
`ApplicationModule` 申明其内部 Controllers 与 Providers 后,就可以在 Controllers 中注入 Providers 了:

```typescript
@Controller()
Expand Down

0 comments on commit d81e6d7

Please sign in to comment.