@xlit/di
TypeScript icon, indicating that this package has built-in type declarations

2.8.1 • Public • Published

@xlit/di

Vanilla dependency injection custom element.

Installation

npm i @xlit/di

Getting started

Dependency injection container works for custom elements.

import { Container, instance, singleton } from '@xlit/di';

// provide value for key programmatically
const container = Container.instance()
  .provide('foo', () => 'foo')
  .provide('bar', instance('bar'))
  .provide('baz', singleton(() => 'baz'));

// lookup valie from container programmatically
const foo = await container.lookup('foo');

Use decorators to provide and lookup.

import { provide, lookup } from '@xlit/di';

@provide()
class FooService {
  doFoo() [
    // do foo
  ]
}

class XApp extends HTMLElement {
  @lookup()
  fooService!: FooService;

  doSomethingWithFooService() {
    this.fooService.doFoo();
  }
}
customElements.define('x-app', XApp);

Package Sidebar

Install

npm i @xlit/di

Weekly Downloads

6

Version

2.8.1

License

MIT

Unpacked Size

9.67 kB

Total Files

11

Last publish

Collaborators

  • reekoheek