Comments (13)
Did you add import "reflect-metadata"; ?
from typedi.
When using classes, you don't need to use @Inject
when injecting in the constructor. You simply write:
constructor(
private userService: UserService,
private loyaltyService: LoyaltyService,
private timerService: TimerService,
) { }
I don't know about using them with default exports for other modules, but why do you want to inject them at all? You can just start using them right away.
import * as moment from 'moment';
// ....
@Service('AccessTokenService')
export class AccessTokenService {
//...
public myMethod() {
return moment().format();
}
}
from typedi.
Its strange that second is case is working it shall not work as well. Probably second case is a bug.
Your problem is that you mark service with a label, you need to call:
const accessTokenService = Container.get<AccessTokenService>('AccessTokenService'); // string here is a name of the service you provided in the decorator
accessTokenService.makeAccessToken(user);
instead of what you are doing right now. If you want to get service by a class then you need to remove service's label.
from typedi.
Thank you. As i see the type variable must be supplied when using get with string aliases.
from typedi.
I'm afraid this solution Container.get<AccessTokenService>('AccessTokenService')
still does not inject the services into the constructor. I even removed the Require
decorator and replaced it with Inject
.
What am i missing here?
import {Container} from 'typedi';
import {AccessTokenService} from 'src/authentication/services/access-token-service';
import * as cfg from 'src/application/config';
import * as moment from 'moment';
import * as jsonwebtoken from 'jsonwebtoken';
Container.set('cfg.auth.jwt', cfg.auth.jwt);
Container.set('moment', moment)
Container.set('jsonwebtoken', jsonwebtoken);
const accessTokenService = Container.get<AccessTokenService>('AccessTokenService');
Again, this does not work
import {Service, Inject} from 'typedi';
//..
@Service('AccessTokenService')
export class AccessTokenService {
public constructor(
@Inject('moment') private moment,
@Inject('jsonwebtoken') private jsonwebtoken,
@Inject('cfg.auth.jwt') private jwt
) {
console.log(moment); //undefined
console.log(jsonwebtoken); //undefined
console.log(jwt); //undefined
}
//..
}
but this does
import {Service, Inject} from 'typedi';
//..
@Service('AccessTokenService')
export class AccessTokenService {
@Inject('moment')
private moment;
@Inject('jsonwebtoken')
private jsonwebtoken;
@Inject('cfg.auth.jwt')
private jwt;
//..
}
from typedi.
When using classes, you don't need to use @Inject when injecting in the constructor. You simply write:
In your example you are using concrete class implementations. But sometimes i also want my dependencies to be interface binded. That would not work, because there is now way to know which implementation to load. That is also why i use the service name in the @Service
@Inject
decorators.
I don't know about using them with default exports for other modules, but why do you want to inject them at all? You can just start using them right away.
I do this to make testing easier. If i want to mock out moment, it is a lot easier this way. In plain javascript i could also use rewire but this does not work well with a typescript setup.
from typedi.
I do this to make testing easier
That makes sense, but then cannot you do this?
constructor(
private momment: moment = moment,
) { }
No need for dependency injections, you still can mock it.
from typedi.
I could also do it that way.
Actually for testing this also works for me, because i can set the dependcies manually in the container
@Service('AccessTokenService')
export class AccessTokenService {
@Inject('moment')
private moment;
@Inject('jsonwebtoken')
private jsonwebtoken;
@Inject('cfg.auth.jwt')
private jwt;
//..
}
My only problem is why it works on the properties but not in the constructor. This seems like an inconsistency. It should either work with both or work with neither.
from typedi.
@ibox4real can you please provide a demo git repo with minimal reproduction of this problem? I added test (see commit above) and it does not reproduce your issue
from typedi.
ping @ibox4real
from typedi.
Yes, i will try to reproduce it in a simplified setup within the next few days.
from typedi.
yeah, probably he forgot to import reflect-metadata. Closing.
from typedi.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
from typedi.
Related Issues (20)
- question: How do you test your services with dependency injection? HOT 32
- fix: Constructor Injection fail in vitest/jest HOT 3
- question: adding custom proxy traps in class constructors and instances HOT 2
- fix: non-reconstructable services are erased when imported HOT 1
- question: Stage 3 decorator support (available since TS 5.0)
- question: How to debug circular dependency injections ?
- Question: Service Injection working incorrectly HOT 1
- question: is this project/repo dead? HOT 5
- question: <Implementing repository injection using typedi-typeorm.> HOT 3
- question: <Is it possible to adapt your DI to work with modules (not classes)?> HOT 1
- question: HOT 3
- fix: Error when running in bunjs runtime HOT 2
- fix: not working function dependency injection examples from docs HOT 3
- question: Inject Private Property HOT 2
- Using ContainerInstance in the service constructor causes TypeScript build breaks HOT 1
- question: identifier was not found in the container HOT 2
- question: Using typedi with monorepo HOT 3
- question: Using Typedi with Vite - Returning ContainerInstance2 HOT 3
- question: How to define a third-party dependency HOT 5
- question: Using Token with Generics failed in stongly typying
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typedi.