-
-
Notifications
You must be signed in to change notification settings - Fork 10
Dependency Injection
Qian.Sicheng edited this page May 27, 2021
·
1 revision
依赖项是指某个类执行其功能所需的服务或对象。依赖项注入(DI)是一种设计模式,在这种设计模式中,类会从外部源请求依赖项而不是创建它们。
Cosmic DI 框架会在实例化某个 Service 类或组件时为其提供依赖。
Cosmic DI 的核心由 InversifyJS 实现,你可以参考 InversifyJS 来创建、注册Service,以及在模块和组件中获取 DI 实例。
/** color-set.ts **/
@injectable()
export class ColorSet {
constructor(
@inject(ColorSetRoot) private root: HTMLElement,
@inject(AppearanceService) private aps: AppearanceService,
) {
// ..
}
// ..
}
<script lang="ts">
import inject from '@cosmic-base/parts/ioc/inject';
import { AppearanceService } from '@cosmic-workbench/services/appearance-service';
const aps: AppearanceService = inject(AppearanceService);
</script>
AppearanceType is {aps.type()}
Project Management
- Source Code Organization
- Development Framework
- User Interface