<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## 模塊參考 Nest提供了一個`ModuleRef`類來導航到內部提供者列表,并使用注入令牌作為查找鍵名來獲取一個引用。`ModuleRef`類也提供了一個動態實例化靜態和范圍的提供者的方法。`ModuleRef`可以通過常規方法注入到類中: >cats.service.ts ```typescript @Injectable() export class CatsService { constructor(private moduleRef: ModuleRef) {} } ``` > `ModuleRef`從`@nestjs/core`中引入。 ### 獲取實例 `ModuleRef`實例(下文稱為**模塊引用**) 擁有`get()`方法。該方法獲取一個提供者,控制器或者通過注入令牌/類名獲取一個在當前模塊中可注入對象(例如守衛或攔截器等)。 >cats.service.ts ```typescript @Injectable() export class CatsService implements OnModuleInit { private service: Service; constructor(private moduleRef: ModuleRef) {} onModuleInit() { this.service = this.moduleRef.get(Service); } } ``` > 不能通過`get()`方法獲取一個范圍的提供者(暫態的或者請求范圍的)。要使用下列的技術,參考[這里](https://docs.nestjs.com/fundamentals/injection-scopes)了解更多控制范圍。 要從全局上下文獲取一個提供者(例如,如果提供者在不同模塊中注入),向`get()`的第二個參數傳遞`{ strict: false }`選項。 ```typescript this.moduleRef.get(Service, { strict: false }); ``` ### 處理范圍提供者 要動態處理一個范圍提供者(瞬態的或請求范圍的),使用`resolve()`方法并將提供者的注入令牌作為參數提供給方法。 >cats.service.ts ```typescript @Injectable() export class CatsService implements OnModuleInit { private transientService: TransientService; constructor(private moduleRef: ModuleRef) {} async onModuleInit() { this.transientService = await this.moduleRef.resolve(TransientService); } } ``` `resolve()`方法從其自身的注入容器樹返回一個提供者的唯一實例。每個子樹都有一個獨一無二的上下文引用。因此如果你調用該方法一次以上并進行引用比較的話,結果是不同的。 >cats.service.ts ```typescript @Injectable() export class CatsService implements OnModuleInit { constructor(private moduleRef: ModuleRef) {} async onModuleInit() { const transientServices = await Promise.all([ this.moduleRef.resolve(TransientService), this.moduleRef.resolve(TransientService), ]); console.log(transientServices[0] === transientServices[1]); // false } } ``` 要在不同的`resolve()`調用之間產生一個單例,并保證他們共享同樣生成的DI容器子樹,向`resolve()`方法傳遞一個上下文引用,使用`ContextIdFactory`類來生成上下文引用。該類提供了一個`create()`方法,返回一個合適的獨一無二的引用。 >cats.service.ts ```typescript @Injectable() export class CatsService implements OnModuleInit { constructor(private moduleRef: ModuleRef) {} async onModuleInit() { const contextId = ContextIdFactory.create(); const transientServices = await Promise.all([ this.moduleRef.resolve(TransientService, contextId), this.moduleRef.resolve(TransientService, contextId), ]); console.log(transientServices[0] === transientServices[1]); // true } } ``` > `ContextIdFactory`類從`@nestjs/core`包中引入。 ### 注冊`REQUEST`提供者 手動生成的上下文標識符(帶)表示提供程序所在`ContextIdFactory.create()`的 DI 子樹,因為它們不是由 Nest 依賴注入系統實例化和管理的。`REQUEST``undefined` `REQUEST`為手動創建的 DI 子樹注冊自定義對象,使用`ModuleRef#registerRequestByContextId()`方法如下: ```typescript const contextId = ContextIdFactory.create(); this.moduleRef.registerRequestByContextId(/* YOUR_REQUEST_OBJECT */, contextId); ``` ### 獲取當前子樹 **有時,也需要在請求上下文**中獲取一個請求范圍提供者的實例。例如,`CatsService`是請求范圍的,要獲取的`CatsRepository`實例也被標識為請求范圍。要分享同一個注入容器子樹,你需要獲取當前上下文引用而不是生成一個新的(像前面的`ContextIdFactory.create()`函數)。使用`@Inject()`來獲取當前的請求對象。 >cats.service.ts ```typescript @Injectable() export class CatsService { constructor( @Inject(REQUEST) private request: Record<string, unknown>, ) {} } ``` > 從[這里](https://docs.nestjs.com/fundamentals/injection-scopes#request-provider)了解更多請求提供者 使用`ContextIdFactory`類的`getByRequest()`方法來基于請求對象創建一個上下文id 并傳遞`resolve()`調用: ```typescript const contextId = ContextIdFactory.getByRequest(this.request); const catsRepository = await this.moduleRef.resolve(CatsRepository, contextId); ``` ### 動態實例化自定義類 要動態實例化一個之前未注冊的類作為提供者,使用模塊引用的`create()`方法。 > cats.service.ts ```typescript @Injectable() export class CatsService implements OnModuleInit { private catsFactory: CatsFactory; constructor(private moduleRef: ModuleRef) {} async onModuleInit() { this.catsFactory = await this.moduleRef.create(CatsFactory); } } ``` 該技術允許你在框架容器之外偶然實例化一個不同的類。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看