Next 内在
实现原理
通过<font style="color:rgb(37, 41, 51);">Reflect.getMetadata、Reflect.defineMetadata</font>
这些 api 给 class 或者对象添加 <font style="color:rgb(37, 41, 51);">metadata</font>
,并且开启 ts 的 <font style="color:rgb(37, 41, 51);">emitDecoratorMetadata</font>
来自动添加类型相关的 <font style="color:rgb(37, 41, 51);">metadata</font>
,然后运行的时候通过这些元数据来实现依赖的扫描,对象的创建等等功能。
Nest 的 <font style="color:rgb(37, 41, 51);">Controller、Module、Service</font>
等等所有的装饰器都是通过 <font style="color:rgb(37, 41, 51);">Reflect.meatdata</font>
给类或对象添加元数据的,然后初始化的时候取出来做依赖的扫描,实例化后放到 IOC 容器里。
Nest 还提供了 **<font style="color:rgb(37, 41, 51);">@SetMetadata</font>**
的装饰器,可以在 <font style="color:rgb(37, 41, 51);">controller</font>
的 class 和 method 上添加 <font style="color:rgb(37, 41, 51);">metadata</font>
,然后在 <font style="color:rgb(37, 41, 51);">interceptor</font>
和 <font style="color:rgb(37, 41, 51);">guard</font>
里通过 **<font style="color:rgb(37, 41, 51);">reflector</font>**
的 api 取出来。
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!