You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 4, 2017. It is now read-only.
Copy file name to clipboardExpand all lines: public/docs/ts/latest/guide/style-guide.jade
+52-50
Original file line number
Diff line number
Diff line change
@@ -37,7 +37,8 @@ include ../_util-fns
37
37
:marked
38
38
## File Structure Conventions
39
39
40
-
Some code examples display a file that has one or more similarly named companion files. (e.g. hero.component.ts and hero.component.html).
40
+
Some code examples display a file that has one or more similarly named companion files.
41
+
For example, `hero.component.ts` and `hero.component.html`.
41
42
42
43
The guideline will use the shortcut `hero.component.ts|html|css|spec` to represent those various files. Using this shortcut makes this guide's file structures easier to read and more terse.
1. [App Structure and Angular Modules](#app-structure-and-angular-modules)
53
+
1. [Coding conventions](#coding-conventions)
54
+
1. [App structure and Angular modules](#app-structure-and-angular-modules)
54
55
1. [Components](#components)
55
56
1. [Directives](#directives)
56
57
1. [Services](#services)
57
-
1. [Data Services](#data-services)
58
-
1. [Lifecycle Hooks](#lifecycle-hooks)
58
+
1. [Data services](#data-services)
59
+
1. [Lifecycle hooks](#lifecycle-hooks)
59
60
1. [Appendix](#appendix)
60
61
61
62
.l-main-section
62
63
:marked
63
64
## Single Responsibility
64
65
65
-
Apply the [Single Responsibility Principle](https://wikipedia.org/wiki/Single_responsibility_principle) to all components, services, and other symbols.
66
+
Apply the [single responsibility principle](https://wikipedia.org/wiki/Single_responsibility_principle) to all components, services, and other symbols.
66
67
This helps make the app cleaner, easier to read and maintain, and more testable.
67
68
68
69
### <a id="01-01"></a>Rule of One
69
70
#### <a href="#01-01">Style 01-01</a>
70
71
.s-rule.do
71
72
:marked
72
-
**Do** define one thing (e.g. service or component) per file.
73
+
**Do** define one thing, such as a service or component, per file.
73
74
74
75
.s-rule.consider
75
76
:marked
76
77
**Consider** limiting files to 400 lines of code.
77
78
78
79
.s-why
79
80
:marked
80
-
**Why?** One component per file makes it far easier to read, maintain, and avoid collisions with teams in source control.
81
+
**Why?** One component per file makes it far easier to read, maintain, and avoid
82
+
collisions with teams in source control.
81
83
82
84
.s-why
83
85
:marked
84
86
**Why?** One component per file avoids hidden bugs that often arise when combining components in a file where they may share variables, create unwanted closures, or unwanted coupling with dependencies.
85
87
86
88
.s-why.s-why-last
87
89
:marked
88
-
**Why?** A single component can be the default export for its file which facilitates lazy loading with the Router.
90
+
**Why?** A single component can be the default export for its file which facilitates lazy loading with the router.
89
91
:marked
90
92
The key is to make the code more reusable, easier to read, and less mistake prone.
91
93
92
94
The following *negative* example defines the `AppComponent`, bootstraps the app, defines the `Hero` model object, and loads heroes from the server ... all in the same file. *Don't do this*.
that humans start to struggle when the number of adjacent interesting things exceeds nine.
@@ -1099,7 +1094,7 @@ a(href="#toc") Back to top
1099
1094
1100
1095
.s-rule.consider
1101
1096
:marked
1102
-
**Consider** creating a folder for a component when is has multiple accompanying files (`.ts`, `.html`, `.css` and `.spec`).
1097
+
**Consider** creating a folder for a component when it has multiple accompanying files (`.ts`, `.html`, `.css` and `.spec`).
1103
1098
1104
1099
.s-why
1105
1100
:marked
@@ -1169,7 +1164,7 @@ a(id='file-tree')
1169
1164
1170
1165
.l-sub-section
1171
1166
:marked
1172
-
While components in dedicated folder are widely preferred,
1167
+
While components in dedicated folders are widely preferred,
1173
1168
another option for small apps is to keep components flat (not in a dedicated folder).
1174
1169
This adds up to four files to the existing folder, but also reduces the folder nesting.
1175
1170
Whatever you choose, be consistent.
@@ -1187,7 +1182,8 @@ a(href="#toc") Back to top
1187
1182
1188
1183
.s-why
1189
1184
:marked
1190
-
**Why?** A developer can locate the code, identify what each file represents at a glance, the structure is as flat as it can be, and there is no repetitive nor redundant names.
1185
+
**Why?** A developer can locate the code, identify what each file represents
1186
+
at a glance, the structure is as flat as it can be, and there are no repetitive nor redundant names.
1191
1187
1192
1188
.s-why
1193
1189
:marked
@@ -1214,7 +1210,7 @@ a(href="#toc") Back to top
1214
1210
**Why?** Angular modules make it easier to isolate, test, and re-use features.
1215
1211
1216
1212
.file-tree-reference
1217
-
a(href="#file-tree") Refer here to this Folder and File Structure example
1213
+
a(href="#file-tree") Refer to this Folder and File Structure example
1218
1214
1219
1215
a(href="#toc") Back to top
1220
1216
:marked
@@ -1303,7 +1299,7 @@ a(href="#toc") Back to top
1303
1299
1304
1300
.s-rule.do
1305
1301
:marked
1306
-
**Do** put common components, directives and pipes that will be used throughout the application by other feature modules in the `SharedModule`, where those assets are expected to share a new instance of themselves (not singletons).
1302
+
**Do** put common components, directives, and pipes that will be used throughout the application by other feature modules in the `SharedModule`, where those assets are expected to share a new instance of themselves (not singletons).
1307
1303
1308
1304
.s-rule.do
1309
1305
:marked
@@ -1384,7 +1380,7 @@ a(href="#toc") Back to top
1384
1380
1385
1381
.s-rule.do
1386
1382
:marked
1387
-
**Do** collect single-use classes and hiding their gory details inside `CoreModule`. A simplified root `AppModule` imports `CoreModule` in its capacity as orchestrator of the application as a whole.
1383
+
**Do** collect single-use classes and hide their gory details inside `CoreModule`. A simplified root `AppModule` imports `CoreModule` in its capacity as orchestrator of the application as a whole.
1388
1384
1389
1385
.s-rule.do
1390
1386
:marked
@@ -1431,7 +1427,7 @@ a(href="#toc") Back to top
1431
1427
1432
1428
.s-rule.do
1433
1429
:marked
1434
-
**Do** export all symbols that from the `CoreModule` that the `AppModule` will import and make available for other feature modules to use.
1430
+
**Do** export all symbols from the `CoreModule` that the `AppModule` will import and make available for other feature modules to use.
1435
1431
1436
1432
.s-why
1437
1433
:marked
@@ -1692,7 +1688,7 @@ a(href="#toc") Back to top
1692
1688
.s-why
1693
1689
:marked
1694
1690
**Why?** If you ever need to rename the property or event name associated with
1695
-
`@Input` or `@Output`, you can modify it a single place.
1691
+
`@Input` or `@Output`, you can modify it in a single place.
1696
1692
1697
1693
.s-why
1698
1694
:marked
@@ -1721,7 +1717,8 @@ a(href="#toc") Back to top
1721
1717
1722
1718
.s-why.s-why-last
1723
1719
:marked
1724
-
**Why?** May lead to confusion when the output or the input properties of a given directive are named a given way but exported differently as a public API.
1720
+
**Why?** May lead to confusion when the output or the input
1721
+
properties of a given directive are named one way but exported differently as a public API.
**Why?** The Angular DI mechanism resolves all dependencies of services based on their types declared with the services' constructors.
2022
+
**Why?** The Angular Dependency Injection (DI) mechanism resolves all dependencies
2023
+
of services based on their types declared with the services' constructors.
2026
2024
2027
2025
.s-why.s-why-last
2028
2026
:marked
@@ -2061,7 +2059,11 @@ a(href="#toc") Back to top
2061
2059
2062
2060
.s-why.s-why-last
2063
2061
:marked
2064
-
**Why?** Data service implementation may have very specific code to handle the data repository. This may include headers, how to talk to the data, or other services such as `Http`. Separating the logic into a data service encapsulates this logic in a single place hiding the implementation from the outside consumers (perhaps a component), also making it easier to change the implementation.
2062
+
**Why?** Data service implementation may have very specific code to handle the data repository.
2063
+
This may include headers, how to talk to the data, or other services such as `Http`.
2064
+
Separating the logic into a data service encapsulates this logic in a
2065
+
single place and hides the implementation from the outside consumers
2066
+
(perhaps a component). This also makes it easier to change the implementation.
2065
2067
2066
2068
a(href="#toc") Back to top
2067
2069
@@ -2084,7 +2086,7 @@ a(href="#toc") Back to top
2084
2086
2085
2087
.s-why.s-why-last
2086
2088
:marked
2087
-
**Why?** Strongly-typed method signatures.
2089
+
**Why?** Lifecycle hook interfaces use strongly-typed method signatures.
2088
2090
The compiler and editor can call out misspellings.
0 commit comments