2017-10-17 87 views
-1

我正在使用Angular 2/4。我有功能模塊,其中包含與該功能對應的所有內容。例如,我有一個建築模塊和一個客戶端模塊。如何訪問另一個功能模塊中的服務?

Example of folder structure for Building Module

這基本上是我的客戶端功能模塊相同的結構。

現在在我的建築模塊,我需要爲我所需要的客戶名單,因爲他們與建築相關的訪問ClientService。我可以像這樣導入客戶端服務嗎?

 import { BuildingService } from "../buildingservice"; 
    import { ClientService } from "../../client/clientservice"; 

    @Component({ 
     selector: 'building-detail', 
     templateUrl: './building-detail.component.html', 
     providers: [BuildingService, ClientService] 
    }) 

    export class BuildingDetailComponent extends ComponentBase { 

     constructor(private buildingService: BuildingService, private clientService: ClientService) { 
    super(); 
} 
    } 

如果我把我的ClientService放到SharedServices文件夾中,還是可以嗎?

回答

1

的服務工作,它應該只被添加到您選擇的任何模塊的供應商陣列,因爲它確實取決於你想如何構建你的項目,因爲這不會有任何影響功能上。

該服務將在你注入到是否有人建築模塊或客戶端模塊中註冊的服務的任何組件工作。

2

你如何組織你的代碼是完全取決於你。通過複雜的路徑導入模塊沒有任何問題。

如果某些代碼是很遙遠的模塊之間共享,它可能是有意義的反映,在代碼是如何組織的。這樣的重命名可以「共享」並將其放置在更高的目錄中。但同樣,你決定:)

相關問題