https://github.com/emmat-york/angular-style-injector
https://github.com/emmat-york/angular-style-injector
Last synced: 6 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/emmat-york/angular-style-injector
- Owner: emmat-york
- Created: 2025-05-31T13:50:27.000Z (19 days ago)
- Default Branch: develop
- Last Pushed: 2025-06-08T21:53:34.000Z (11 days ago)
- Last Synced: 2025-06-08T22:27:50.438Z (11 days ago)
- Language: TypeScript
- Size: 15.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- fucking-awesome-angular - angular-style-injector - A lightweight dependency injection container inspired by Angular's Injector. (Table of contents / Third Party Components)
- awesome-angular - angular-style-injector - A lightweight dependency injection container inspired by Angular's Injector. (Table of contents / Third Party Components)
README
# Angular-Style Injector
A lightweight dependency injection container inspired by Angular's Injector.
## Installation
```bash
npm install angular-style-injector
```## Preconditions
Before you start using this package, make sure to complete the following steps first:
1. Import `reflect-metadata` once in your entry point `.ts` file:
```ts
import 'reflect-metadata';
```2. Add these two essential parameters to your `tsconfig.json` file:
```json
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
```## Usage
```ts
import { Injector, Injectable, InjectionToken } from 'angular-style-injector';@Injectable()
class DependencyOne {
readonly description = 'Dependency one';
}@Injectable()
class DependencyTwo {
readonly description = 'Dependency two';
}@Injectable()
class Parent {
readonly description = 'Parent';constructor(
readonly dependencyOne: DependencyOne,
readonly dependencyTwo: DependencyTwo,
) {}
}const CLASS_TOKEN = new InjectionToken('CLASS_TOKEN');
const VALUE_TOKEN = new InjectionToken('VALUE_TOKEN');
const FACTORY_TOKEN = new InjectionToken('FACTORY_TOKEN');
const EXISTING_TOKEN = new InjectionToken('EXISTING_TOKEN');const injector = Injector.create({
providers: [
DependencyOne,
DependencyTwo,
{ provide: CLASS_TOKEN, useClass: Parent },
{ provide: VALUE_TOKEN, useValue: 10, multi: true },
{ provide: VALUE_TOKEN, useValue: 20, multi: true },
{ provide: EXISTING_TOKEN, useExisting: VALUE_TOKEN },
],
parent: Injector.create({
providers: [
DependencyTwo,
{
provide: FACTORY_TOKEN,
useFactory: (dependencyTwo: DependencyTwo) => dependencyTwo.description,
deps: [DependencyTwo],
},
],
name: 'parentInjector',
}),
name: 'elementInjector',
});console.log(
injector.get(CLASS_TOKEN), // instance of Parent class
injector.get(VALUE_TOKEN), // Array: [10, 20]
injector.get(FACTORY_TOKEN), // String: 'Dependency two'
injector.get(EXISTING_TOKEN), // Array: [10, 20]
);
```