# Opts Decorator

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

# Overview

function Opts(target: any, propertyKey: undefined, index: number): void;

# Description

Get instance options. This options depending on his invocation context.

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 decorator on a constructor parameter change the Scope of the provider to ProviderScope.INSTANCE.