簡體   English   中英

typescript 依賴注入框架

[英]typescript dependency injection framework

我正在使用 TypeScript(不使用角度)編寫一個簡單的庫。 我需要在這個庫中使用 DI 框架。 我檢查了 Angular2 中的 DI 框架,但這似乎有點過頭了,我需要將我的整個庫包裝在一個我不想做的 Angular 模塊中。

我還沒有在 TypeScript/ES6 中使用 DI 進行生產,因為它們還沒有最終確定。

為了進行實驗,我使用了當時使用 ES6 的針尖

但是對於 typescript, typescript-ioc看起來是一個更好的候選者。 它同時具有 IOC 和 DI。

Dime是我制作的一個簡單的依賴注入庫。 它的靈感來自 Angular 的依賴注入系統。 它仍在開發中,因此可能存在一些錯誤。 您可以在wiki上找到更多信息。

這是一個簡單的使用示例:

import { ItemsService } from './items-service'; // ItemsService is an interface
import { Inject } from '@coined/dime';

class ItemsWidget {
    @Inject()
    private itemsService: ItemsService;

    render() {
        this.itemsService.getItems().subscribe(items => {
            // ...
        })
    }
}

// Setup
const appPackage = new Package("App", {
    token: "itemsService",
    provideClass: AmazonItemsService // AmazonItemsService implements ItemsService
});

Dime.mountPackages(appPackage);

// Display the widget
const widget = new ItemsWidget();
widget.render();

您可能必須在tsconfig.json中禁用嚴格的屬性初始化程序才能使其正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM