IT박스

Angular 2에서 어떤 유형의 폴더 구조를 사용해야합니까?

itboxs 2020. 7. 5. 08:16
반응형

Angular 2에서 어떤 유형의 폴더 구조를 사용해야합니까?


저는 Angular 2에 대해 배우기 시작한 Angular 1 개발자입니다. 교육 자료에 따라 다양한 유형의 폴더 구조 방법이 있습니다. 아래에 각 항목을 나열하고 사용해야 할 사람들의 의견과 그 이유를 알고 싶습니다. 또한 나열되지 않은 방법이 있지만 더 잘 작동한다고 생각되면 자유롭게 나열하십시오.

이 모든 것을 살펴보면 방법 # 3은 Angular 1 앱을 수행하는 방식과 거의 같습니다.

방법 1 : angular2-quickstart

출처 : https://angular.io/guide/quickstart

폴더 구조 :

여기에 이미지 설명을 입력하십시오

방법 2 : ng-book2

출처 : https://www.ng-book.com/2/ (파일을 보려면 비용을 지불해야 함)

폴더 구조 :

여기에 이미지 설명을 입력하십시오

방법 3 : mgechev / angular2-seed

출처 : https://github.com/mgechev/angular2-seed

폴더 구조 :

여기에 이미지 설명을 입력하십시오


공식적인 지침이 있습니다. mgechev/angular2-seed그것과도 일치했다. # 857 참조 .

각도 2 응용 구조

https://angular.io/guide/styleguide#overall-structural-guidelines


기능별로 프로젝트를 구성하는 것이 실용적인 방법이라고 생각합니다. 프로젝트를 확장 가능하고 유지 관리하기 쉽게 만듭니다. 그리고 그것은 프로젝트의 각 부분이 완전히 자율적으로 작동하게합니다. 아래의이 구조에 대해 어떻게 생각하는지 알려주세요 : ANGULAR TYPESCRIPT PROJECT STRUCTURE – ANGULAR 2

출처 : http://www.angulartypescript.com/angular-typescript-project-structure/


그래서 더 많은 조사를 한 후에 약간 수정 된 버전 3의 방법 (mgechev / angular2-seed)을 사용하게되었습니다.

기본적으로 구성 요소를 기본 레벨 디렉토리로 옮긴 다음 각 기능이 그 내부에있게됩니다.


나는 이것을 사용할 것입니다. @Marin이 보여주는 세 번째 것과 매우 유사합니다.

app
|
|___ images
|
|___ fonts
|
|___ css
|
|___ *main.ts*
|   
|___ *main.component.ts*
|
|___ *index.html*
|
|___ components
|   |
|   |___ shared
|   |
|   |___ home
|   |
|   |___ about
|   |
|   |___ product
|
|___ services
|
|___ structures

아마도이 구조와 같은 것 :

|-- app
     |-- modules
       |-- home
           |-- [+] components
           |-- pages
              |-- home
              |-- home.component.ts|html|scss|spec
           |-- home-routing.module.ts
           |-- home.module.ts
     |-- core
       |-- authentication
           |-- authentication.service.ts|spec.ts
       |-- footer
           |-- footer.component.ts|html|scss|spec.ts
       |-- guards
           |-- auth.guard.ts
           |-- no-auth-guard.ts
           |-- admin-guard.ts 
       |-- http
           |-- user
               |-- user.service.ts|spec.ts
           |-- api.service.ts|spec.ts
       |-- interceptors
           |-- api-prefix.interceptor.ts
           |-- error-handler.interceptor.ts
           |-- http.token.interceptor.ts
       |-- mocks
           |-- user.mock.ts
       |-- services
           |-- srv1.service.ts|spec.ts
           |-- srv2.service.ts|spec.ts
       |-- header
           |-- header.component.ts|html|scss|spec.ts
       |-- core.module.ts
       |-- ensureModuleLoadedOnceGuard.ts
       |-- logger.service.ts
     |-- shared
          |-- components
              |-- loader
                  |-- loader.component.ts|html|scss|spec.ts
          |-- buttons
              |-- favorite-button
                  |-- favorite-button.component.ts|html|scss|spec.ts
              |-- collapse-button
                  |-- collapse-button.component.ts|html|scss|spec.ts
          |-- directives
              |-- auth.directive.ts|spec.ts
          |-- pipes
              |-- capitalize.pipe.ts
              |-- safe.pipe.ts
     |-- configs
         |-- app-settings.config.ts
         |-- dt-norwegian.config.ts
     |-- scss
          |-- [+] partials
          |-- _base.scss
          |-- styles.scss
     |-- assets

최근에 ng cli를 사용하고 있으며 코드를 구성하는 좋은 방법을 찾기가 정말 어려웠습니다.

지금까지 본 가장 효율적인 방법은 mrholek 저장소 ( https://github.com/mrholek/CoreUI-Angular )에서 제공됩니다.

이 폴더 구조를 사용하면 루트 프로젝트를 깨끗하게 유지하고 구성 요소를 구조화 할 수 있으며 공식 스타일 가이드의 중복 (때로는 쓸모없는) 명명 규칙을 피할 수 있습니다.

또한이 구조는 필요할 때 가져 오기를 그룹화하고 단일 파일에 대해 30 줄의 가져 오기를 피하는 데 유용합니다.

src
|
|___ app
|
|   |___ components/shared
|   |   |___ header
|   |
|   |___ containers/layout
|   |   |___ layout1
|   |
|   |___ directives
|   |   |___ sidebar
|   |
|   |___ services
|   |   |___ *user.service.ts* 
|   | 
|   |___ guards
|   |   |___ *auth.guard.ts* 
|   |
|   |___ views
|   |   |___ about  
|   |
|   |___ *app.component.ts*
|   |
|   |___ *app.module.ts*
|   |
|   |___ *app.routing.ts*
|
|___ assets
|
|___ environments
|
|___ img
|   
|___ scss
|
|___ *index.html*
|
|___ *main.ts*

프로젝트가 작고 작게 유지되면 유형별로 구조화하는 것이 좋습니다 (방법 2 : ng-book2)

app
|- components
|  |- hero
|  |- hero-list
|  |- villain
|  |- ...
|- services
|  |- hero.service.ts
|  |- ...
|- utils
|- shared

프로젝트가 커질 경우 도메인별로 폴더를 구성해야합니다 (방법 3 : mgechev / angular2-seed)

app
|- heroes
|  |- hero
|  |- hero-list
|  |- hero.service.ts
|- villains
|  |- villain
|  |- ...
|- utils
|- shared

공식 문서를 따르는 것이 좋습니다.
https://angular.io/guide/styleguide#application-structure-and-ngmodules


기존의 일부 규칙을 위반할 수있는 다음 구조를 제안합니다.

나는 경로에서 이름 중복을 줄이기 위해 노력하고 있었고 일반적으로 이름을 짧게 유지하려고했습니다.

따라서 /app/components/home/home.component.ts|html|css가 없습니다.

대신 다음과 같이 보입니다.

|-- app
    |-- users
        |-- list.ts|html|css
        |-- form.ts|html|css
    |-- cars
        |-- list.ts|html|css
        |-- form.ts|html|css
        |-- configurator.ts|html|css
    |-- app.component.ts|html|css
    |-- app.module.ts
    |-- user.service.ts
    |-- car.service.ts
|-- index.html
|-- main.ts
|-- style.css

참고 URL : https://stackoverflow.com/questions/35346342/which-type-of-folder-structure-should-be-used-with-angular-2

반응형