Angular Style Guide

Angular Syntax, 규칙 및 응용 프로그램 구조에 대한 독창적 인 가이드를 찾고 계십니까? 바로 들어가십시오! 이 스타일 가이드는 선호하는 규칙을 제시하며, 중요한 이유를 설명합니다.

Style vocabulary

각 guideline은 좋은 사례, 나쁜 사례를 설명하고 일관된 설명을 제공합니다.

각 지침의 표현은 얼마나 강력한 권고인지 나타냅니다.:

File structure conventions

일부 코드 예제는 하나 이상의 유사한 이름의 짝 파일이있는 파일을 표시합니다. 예 : hero.component.tshero.component.html.

지침은 바로 가기 hero.component.ts|html|css|spec을 사용하여 다양한 파일을 나타냅니다. 이 바로 가기를 사용하면이 안내서의 파일 구조를 읽기 쉽고 간결하게 만들 수 있습니다.

Single responsibility

Single Responsibility Principle(SRP:단일 책임 원칙)을 모든 구성 요소, 서비스 및 기타 기호에 적용하십시오. 이렇게하면 앱을보다 깔끔하고 쉽게 읽고 유지 관리 할 수 있으며 테스트 할 수 있습니다.

Rule of One

Style 01-01

Small functions

Style 01-02

Naming

이름 지정 규칙은 유지 보수성과 가독성을 위해 대단히 중요합니다. 이 설명서는 파일 이름과 기호 이름에 대해 이름 지정 규칙을 권장합니다.

General Naming Guidelines

Style 02-01

Separate file names with dots and dashes

Style 02-02

Symbols and file names

Style 02-03

Service Names

Style 02-04

약간의 용어는 모호하지 않은 서비스입니다. 그들은 일반적으로 -er로 끝나는 대행사를 나타냅니다. LoggerService 대신 Logger 메시지를 기록하는 서비스의 이름을 지정할 수도 있습니다. 프로젝트에서이 예외가 동의 할 지 결정하십시오. 항상 그렇듯이 일관성을 위해 노력하십시오.

Symbol Name File Name
@Injectable()
export class HeroDataService { }
hero-data.service.ts
@Injectable()
export class CreditService { }
credit.service.ts
@Injectable()
export class Logger { }
logger.service.ts

Bootstrapping

Style 02-05

Component selectors

Style 02-06

Component custom prefix

Style 02-07

Directive selectors

Style 02-07

Directive custom prefix

Style 02-08

Pipe names

Style 02-09

Unit test file names

Style 02-10

End-to-End (E2E) test file names

Style 02-11

Angular NgModule names

Style 02-12

Coding conventions

코딩, 명명 및 공백 규칙을 일관되게 설정하십시오.

Classes

Style 03-01

Constants

Style 03-02

Interfaces

Style 03-03

import { Hero } from ‘./hero.model’;

@Injectable() export class HeroCollectorService { hero: Hero;

constructor() { } } ```

Properties and methods

Style 03-04

Import line spacing

Style 03-06

Application structure and NgModules

장기적인 비전과 실행에 대한 단기적인 관점을가집니다. 작게 시작하지만 앱이 어디로 향하는 지 명심하십시오.

모든 앱 코드는 src 폴더에 있습니다. 모든 기능 영역은 자체 NgModule과 함께 자체 폴더에 있습니다.

모든 콘텐츠는 파일 당 하나의 저작물입니다. 각 구성 요소, 서비스 및 파이프는 자체 파일에 있습니다. 모든 타사 공급 업체 스크립트는 src 폴더가 아닌 다른 폴더에 저장됩니다. 당신은 그 (것)들을 쓰지 않으며 당신은 그 (것)들을 src를 어수선하게하고 싶지 않다. 이 설명서의 파일에는 이름 지정 규칙을 사용하십시오.

LIFT

Style 04-01

Locate

Style 04-02

Identify

Style 04-03

하나의 파일에서 여러 개의 파일보다 더 잘 발견되고 이해되는 작고 밀접하게 관련된 기능 세트를 가지고있을 때 파일 당 일 (one-thing-per-file) 규칙을 벗어나는 것이 유리할 수 있습니다. 이 허점을 조심하십시오.

Flat

Style 04-04

On the other hand, psychologists believe that humans start to struggle when the number of adjacent interesting things exceeds nine. So when a folder has ten or more files, it may be time to create subfolders.

Base your decision on your comfort level. Use a flatter structure until there is an obvious value to creating a new folder.

T-DRY (Try to be DRY)

Style 04-05

Overall structural guidelines

Style 04-06

전용 폴더의 구성 요소가 널리 선호되는 반면, 소형 응용 프로그램의 또 다른 옵션은 구성 요소를 편평하게 유지하는 것입니다 (전용 폴더가 아님). 이렇게하면 최대 4 개의 파일이 기존 폴더에 추가되지만 폴더 중첩도 줄어 듭니다. 당신이 선택한 것이 무엇이든지간에 일관성을 유지하십시오.

Folders-by-feature structure

Style 04-07

App root module

Style 04-08

Feature modules

Style 04-09

Shared feature module

Style 04-10

Core feature module

Style 04-11

Prevent re-import of the core module

Style 04-12

Only the root AppModule should import the CoreModule.

Lazy Loaded folders

Style 04-13

A distinct application feature or workflow may be lazy loaded or loaded on demand rather than when the application starts.

Never directly import lazy loaded folders

Style 04-14

Components

Components as elements

Style 05-03

There are a few cases where you give a component an attribute, such as when you want to augment a built-in element. For example, Material Design uses this technique with <button mat-button>. However, you wouldn’t use this technique on a custom element.

Extract templates and styles to their own files

Style 05-04

Decorate input and output properties

Style 05-12

Avoid aliasing inputs and outputs

Style 05-13

Member sequence

Style 05-14

Delegate complex component logic to services

Style 05-15

Don’t prefix output properties

Style 05-16

Put presentation logic in the component class

Style 05-17

Directives

Use directives to enhance an element

Style 06-01

HostListener/HostBinding decorators versus host metadata

Style 06-03

Compare with the less preferred host metadata alternative.

Services

Services are singletons

Style 07-01

Single responsibility

Style 07-02

Providing a service

Style 07-03

Use the @Injectable() class decorator

Style 07-04

Data Services

Talk to the server through a service

Style 08-01

A data service encapsulates these details. It’s easier to evolve these details inside the service without affecting its consumers. And it’s easier to test the consumers with mock service implementations.

Lifecycle hooks

Use Lifecycle hooks to tap into important events exposed by Angular.

Implement lifecycle hook interfaces

Style 09-01

Appendix

Angular에 유용한 도구 및 팁.

Codelyzer

Style A-01

File templates and snippets

Style A-02