# UseOpts Decorator

Module
import { UseOpts } from "@tsed/di"
Source/packages/di/src/decorators/useOpts.ts

# Overview

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

# Description

Add options to invoke the Service.

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
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

WARNING

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

:::