Skip to content

Instantly share code, notes, and snippets.

@AhnMo
Created March 12, 2021 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AhnMo/6db3d5856fb5b3f27cbc73f8f725cd78 to your computer and use it in GitHub Desktop.
Save AhnMo/6db3d5856fb5b3f27cbc73f8f725cd78 to your computer and use it in GitHub Desktop.
import { Service } from 'typedi'
@Service()
export class ExampleInjectedService {
printMessage() {
console.log('I am alive!')
}
}
import 'reflect-metadata'
import { Service } from 'typedi'
import { ExampleInjectedService } from './bar';
import * as _ from 'lodash'
@Service()
export default class ExampleService {
constructor(
public injectedService: ExampleInjectedService
) {}
public test() {
console.log(this.injectedService)
this.injectedService.printMessage()
}
public test2(compareService: ExampleInjectedService) {
console.log(_.isEqual(compareService, this.injectedService))
}
public getService() {
return this.injectedService
}
}
import ExampleService from './baz'
import { Container } from 'typedi'
const serviceInstance = Container.get(ExampleService);
let svc = serviceInstance.getService();
const serviceInstance2 = Container.get(ExampleService);
serviceInstance2.test2(svc);
const serviceInstance3 = Container.get(ExampleService);
serviceInstance3.test2(svc);
serviceInstance.injectedService.printMessage()
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"lib": [
"dom",
"es5",
"scripthost",
"es2015.symbol"
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment