-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
chenjian
committed
Apr 18, 2024
1 parent
7e7bf5e
commit 6a1e4be
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
title: '作用域' # 当前页面内容标题,默认为 Markdown 文件中的第一个 h1 标签内容 | ||
shortTitle: '' # 当前页面的短标题 | ||
description: '' # 当前页面内容描述 | ||
icon: '' # 当前页面图标的 FontClass 或文件路径 (建议填写)。 | ||
author: { | ||
name: 'Cap' | ||
} | ||
isOriginal: true # 当前文章是否为原创。 | ||
date: 2024-04-18 # 写作时间。 | ||
category: '' # 分类 | ||
tag: '' # 标签 | ||
sticky: 100 # 是否在列表中置顶。当填入数字时,数字越大,排名越靠前 | ||
star: true # 是否收藏在博客主题的文章列表中。当填入数字时,数字越大,排名越靠前。 | ||
article: true # 是否将该文章添加至文章列表中 | ||
timeline: true # 是否将该文章添加至时间线中 | ||
image: '' # 设置预览图 (分享图),请填入绝对路径 | ||
editLink: false # 设置横幅图片 (宽屏分享图),请填入绝对路径。 | ||
--- | ||
|
||
## 作用域 | ||
|
||
### Provider 作用域 | ||
|
||
默认使用单例作用域 | ||
|
||
| 类型 | 说明 | | | ||
| -------- | -------- | -------- | | ||
| DEFAULT | 该提供者的单一实例在整个应用程序中共享。实例的生命周期直接与应用程序的生命周期相关联。一旦应用程序启动,所有单例提供者都已被实例化。默认情况下使用单例作用域。 | | | ||
| REQUEST | 为每个传入请求创建一个该提供者的新实例。实例会在请求完成处理后进行垃圾回收 | | | ||
| TRANSIENT | 瞬态提供者在不同的消费者之间不共享。每个注入瞬态提供者的消费者都会收到一个新的、专用的实例。 | | | ||
|
||
```ts | ||
@Injectable({ scope: Scope.REQUEST }) | ||
``` | ||
|
||
## Controller 作用域 | ||
|
||
控制器也可以具有作用域,该作用域适用于在该控制器中声明的所有请求方法处理程序。与提供者作用域类似,控制器的作用域声明了其生命周期。对于基于请求的控制器,为每个传入请求创建一个新实例,并在请求处理完成后进行垃圾回收。 | ||
|
||
```ts | ||
|
||
@Controller({ | ||
path: 'cats', | ||
scope: Scope.REQUEST, | ||
}) | ||
export class CatsController {} | ||
|
||
``` | ||
|
||
## 作用域层级 | ||
|
||
- REQUEST作用域会向注入链上传播,A依赖B,B依赖C,假设B是REQUEST作用域,那么A也会成为REQUEST作用域,但是C不受影响 | ||
|
||
- TRANSIENT作用域不会影响依赖链上 | ||
|
||
## 持久性提供者 | ||
|
||
TODO |