1.0.5 • Published 3 years ago

what-di v1.0.5

Weekly downloads
74
License
MIT
Repository
github
Last release
3 years ago

依赖注入工具

what-di

安装方法:

npm i what-di
# 或
yarn add what-di

使用方法

创建根容器:

一般在应用入口处创建。创建是可以提供默认的providers和modules。

import { createRootContainer } from 'what-id';

createRootContainer({
  providers: [
    {provide: 'config', useValue: {host: '0.0.0.0'}},
  ],
  modules: []
});

运行时注册供应者:

需要动态注册provider时,可以使用该函数将供应者注册到根容器。

import { registerRootProviders } from 'what-id';

registerRootProviders([
  { provide: 'extra', useValue: { publicUrl: '/' },
]);

注入依赖:

import { inject } from 'what-id';

const config = inject('config');

注入模块依赖:

注入模块依赖时,需要提供对应子模块的命名空间。注入器会先在当前模块内查找实例,如果未找到则会继续从父容器内查找,直到找到。找不到时返回undefined。

import { inject, createNamespaceHelpers } from 'what-id';

const config = inject('config', 'module-name');

// 也可以创建版绑定模块名的辅助函数

const { inject } = createNamespaceHelpers('module-name');
const config = inject('config'); // 即可略去模块名

在类中注入依赖:

import { inject, Inject } from 'what-id';

class Example {
  constructor() {
    this.config = inject('config');
  }
}

// 或装饰器模式。实验功能,可能不稳定。

class Example {
  @Inject('config')
  config;
}

创建子容器:

import { createModuleContainer } from 'what-id';

const module = createModuleContainer({
  providers: [
    {provide: 'module', useValue: {host: '0.0.0.0'}},
  ]
});

创建的子模块可以在创建根容器的时候作为参数传递到modules数组中。

供应者类型

import { BaseProvider } from 'what-di';

export type ValueProvider = {
  provide: string;
  useValue: any;
}

export type FactoryProvider = {
  provide: string;
  useFactory: any;
}

export interface ClassProvider {
  provide: string;
  useClass: typeof BaeProvider;
}

// 也可以直接传递一个类到provider数组,但该类需要继承BaseProvider,例如

// import { BaseProvider, createRootContainer } from 'what-di';
class UserService extends BaseProvider {
  // ...
}

createRootContainer({
  providers: [
    UserService, // 直接使用类,注入时可以 inject('UserService') 或 inject(UserService)
  ],
  modules: []
});

如何订阅状态provider实例状态?

如果使用ClassProvider,需要继承BaseProvider,BaseProvider内置subscribe方法提供订阅;setState方法用于设置状态。

如果是ValueProvider或FactoryProvider,则需要自行实现订阅发布规则。

import { BaseProvider, inject } from 'what-di'
type StateType = {
  name: string;
}

class Subscribable extends BaseProvider<StateType> {
  // ...
  fetch() {
    // ... 
    this.setState(userData);
  }
}

// 订阅状态

const Subscribable = inject<Subscribable>(Subscribable);

const unsubscribe = Subscribable.subscribe((state: StateType) => {
  console.log(state);
});

// 在需要时取消订阅
unsubscribe()

附加API(不建议使用)

getRoot(): Container; 获取根容器实例

what-di工具参考文档: https://www.npmjs.com/package/what-di

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago