2016-12-06 98 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/