Skip to content

UseOpts

@tsed/di

Usage

typescript
import { UseOpts } from "@tsed/di";

See /packages/di/src/types/common/decorators/useOpts.ts.

Overview

ts
function UseOpts(options: {
    [key: string]: any;
}): Function;

Description

Add options to invoke the Service.

typescript
import {Injectable, Opts, UseOpts} from "@tsed/di";

@Injectable()
class MyConfigurableService {
  source: string;
  constructor(@Opts options: any = {}) {
     console.log("Hello ", options.source); // log: Hello Service1 then Hello Service2

     this.source = options.source;
  }
}

@Injectable()
class MyService1 {
  constructor(@UseOpts({source: 'Service1'}) service: MyConfigurableService) {
    console.log(service.source) // log: Service1
  }
}

@Injectable()
class MyService2 {
  constructor(@UseOpts({source: 'Service2'}) service: MyConfigurableService) {
    console.log(service.source) // log: Service2
  }
}

WARNING

Using Opts decorator on a constructor parameter change the Scope of the provider to ProviderScope.INSTANCE.

[key: string]

ts
[key: string]: any;

Released under the MIT License.