gpt4 book ai didi

javascript - Angular 2 中推荐的服务架构

转载 作者:行者123 更新时间:2023-11-28 18:31:41 27 4
gpt4 key购买 nike

目前,我将所有服务都放在一个服务目录中,并且多个组件依赖于它们,并且一个服务依赖于另一服务等等。服务和组件的依赖关系图看起来很疯狂。因此,在某些情况下,当尝试将一项服务注入(inject)另一项服务时,我会收到 DI 错误。以下是我的一种情况的示例:

import {stuff} from 'stuffs';

@Injectable()
export class ApiService
{
getData(): Observable<string>{
return this.http.get(url).map(v => v.json());
}
}

@Injectable()
export class SomeService
{
constructor(private apiService: ApiService){

}
}

@Component()
export class SomeComponent
{
constructor(private apiService: ApiService,
private someService: SomeService){

}
}

我没有找到任何组织服务的推荐模式,因此在编写了大量代码后,我请求一些指导,可以为我指明正确的方向并重构它。

最佳答案

Angular 2 网站上有一个样式指南:https://angular.io/docs/ts/latest/guide/style-guide.html#!#application-structure

Do be descriptive with file names and keep the contents of the file to exactly one component.

Avoid files with multiple components, multiple services, or a mixture.

Why? We spend less time hunting and pecking for code, and become more efficient. If this means we want longer file names, then so be it.

而且在小纸条上还写着:

There are deviations of the 1 per file rule when we have a set of very small features that are all related to each other, as they are still easily identifiable.

还有一个总体文件夹和文件结构指南以及更多内容,您可能会在其中找到有趣的内容。

并查看依赖于其他服务的服务的答案:https://stackoverflow.com/a/33979228/5706293

关于javascript - Angular 2 中推荐的服务架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37867264/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com