2016-12-06 30 views
1

我正在關注角度爲todd motto's styleguide。在他的方法和約翰帕帕的方法中,他們說每個組件都應該有自己的服務。角度:使用可重用服務的許多組件

我的問題是,當我有一個服務(例如getArticlesByStoreId),我想從不相互關聯的不同組件使用服務時會發生什麼。從這個風格的指導,我明白我必須重寫我的服務文件到兩個組件,但我想我可以有一個「sharedServices /」爲所有這些共享服務,所以我不會最終重寫代碼。

你會在這種情況下做什麼?

app/ 
|--components/ 
    |--comp1/ 
     |--service.js 
    |--comp2/ 
     |--service.js 

app/ 
|--components/ 
    |--comp1/ 
    |--comp2/ 
|--services/ 
    |--sharedServices/ 
     |--service.js 

回答

1

我使用角1.5和組件架構的大型項目。我們有以下結構,迄今爲止非常好。

└─ src/app/ ├─ app.js # app entry file ├─ app.html # app template ├─ common/ ├─ common.js # common entry file └─ services/ # common services └─ components/ # where components live ├─ components.js # components entry file └─ home/ # home component ├─ home.js # home entry file (routes, configurations etc) ├─ home.component.js # home component definition ├─ home.controller.js # home controller ├─ home.service.js # home service ├─ home.scss # home styles ├─ home.html # home template └─ home.spec.js # home specs (for entry, component, and controller)

1

好吧既然你的問題是我會做什麼,我會做到這一點...結構的特點(約翰·帕帕斯文件夾結構中的一個)我的應用程序。記住每個應用程序可能需要不同的文件夾結我使用下面的文件夾結構來處理中小型應用程序。同樣,您的應用程序需求在這裏很重要。您的應用會增長多少,以及您將使用的文件夾結構的可管理性如何。

app/ 
    |--core 
     |--login.service.js 
    |--feature1/ 
     |--feature1.component.js 
     |--feature1.service.js 
    |--feature2/ 
     |--feature2.service.js 

我喜歡讓我的文件夾結構儘可能平坦。我相信,我閱讀約翰帕帕斯文件夾結構的指導方針,沒有超過2個嵌套文件夾。這裏是他說的文章:https://johnpapa.net/angular-app-structuring-guidelines/