This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| front-end:angular [2020/10/25 12:24] – [created] Creating libraries ledyx | front-end:angular [2023/05/01 14:40] (current) – 문서 통합 ledyx | ||
|---|---|---|---|
| Line 4: | Line 4: | ||
| {{tag> | {{tag> | ||
| + | |||
| + | == Package 구조 == | ||
| + | |||
| + | ^ package.json | ||
| + | ^ tsconfig.json | ||
| + | ^ typings.json | ||
| + | ^ systemjs.config.js | ||
| + | |||
| + | === Package 생성 === | ||
| + | |||
| + | * https:// | ||
| + | |||
| + | |||
| + | ==== 수동 생성 ==== | ||
| + | |||
| + | 1. package.json 생성 | ||
| + | |||
| + | <sxh bash; | ||
| + | npm init | ||
| + | #이후 질문에 따라 입력하면 자동 생성. | ||
| + | #파일 생성 이후 dependencies, | ||
| + | </ | ||
| + | |||
| + | * dependencies : Angular Project 실행에 직접적으로 필요한 필수적인 의존성 패키지 기술 | ||
| + | * devDependencies : Angular Project 개발에 필요한 부가적 패키지 기술 | ||
| + | |||
| + | \\ | ||
| + | 2. 나머지 파일 생성 | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | \\ | ||
| + | 3. 의존성 패키지 설치 | ||
| + | <sxh bash; | ||
| + | npm install | ||
| + | </ | ||
| + | |||
| + | \\ | ||
| + | 4. Project 실행 | ||
| + | <sxh bash; | ||
| + | npm start | ||
| + | </ | ||
| + | |||
| + | |||
| + | == Angular CLI == | ||
| + | |||
| + | 설정보다 **개발**에만 집중할 수 있도록 Project 설정 및 관리를 명령어 기반으로 수행할 수 있도록 지원. | ||
| + | |||
| + | https:// | ||
| + | |||
| + | <sxh bash; | ||
| + | #설치 | ||
| + | npm install -g angular-cli@latest | ||
| + | |||
| + | # | ||
| + | ng new hello-ng2 | ||
| + | |||
| + | #Server 실행 (Default port:4200) | ||
| + | cd hello-ng2 | ||
| + | |||
| + | # 추가 인자: --port 4200 / open | ||
| + | ng serve | ||
| + | </ | ||
| + | |||
| + | <sxh bash; | ||
| + | #Component 추가 | ||
| + | ng g component NAME | ||
| + | |||
| + | #Directive 추가 | ||
| + | ng g directive NAME | ||
| + | |||
| + | #Pipe 추가 | ||
| + | ng g pipe NAME | ||
| + | |||
| + | #Service 추가 | ||
| + | ng g service NAME | ||
| + | |||
| + | #Class 추가 | ||
| + | ng g class NAME | ||
| + | </ | ||
| + | |||
| + | <sxh bash; | ||
| + | #-prod : 배포 환경을 위한 옵션. 파일 크기 최적화 | ||
| + | ng build -prod | ||
| + | |||
| + | #/dist에 결과 생성 | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | = Architecture = | ||
| + | ^ Component | ||
| + | ^ Module | ||
| + | ^ Service | ||
| + | ^ Directive | ||
| + | ^ Router | ||
| + | |||
| + | == Data Binding == | ||
| + | === One way === | ||
| + | |||
| + | ==== Interpolation binding ==== | ||
| + | 삽입식. 문자열로 변환될 수 있는 값을 View에 Binding. Template 표현식에서 사용하는 변수와 함수는 **Component Class의 Context에 포함된 __Attribute__ 및 __Method__**. | ||
| + | |||
| + | * **하지 말아야 할 것** (☞ Component에 작성. 혹은 Data 가공이 필요한 경우 Pipe 이용) | ||
| + | * 시간이 오래걸리는 연산 작성 | ||
| + | * 값을 할당하는 표현식 작성 | ||
| + | |||
| + | <sxh html> | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Property binding ==== | ||
| + | Component에서 DOM의 Property로 값을 Binding. | ||
| + | |||
| + | <sxh html> | ||
| + | <input type=" | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Event binding ==== | ||
| + | View에서 발생하는 Event를 처리할 Logic binding. | ||
| + | |||
| + | https:// | ||
| + | |||
| + | <sxh html> | ||
| + | <input type=" | ||
| + | </ | ||
| + | |||
| + | |||
| + | === Two Way === | ||
| + | |||
| + | <sxh html> | ||
| + | <input type=" | ||
| + | </ | ||
| + | |||
| + | |||
| + | = Component = | ||
| + | |||
| + | == 기초 == | ||
| + | === 명명법 === | ||
| + | * Component 파일명은 " | ||
| + | * ex) myComp.__**component**__.ts | ||
| + | * Component Tag명은 " | ||
| + | * ex) selector:' | ||
| + | |||
| + | === 영역 === | ||
| + | |||
| + | import, Component, class 영역으로 나눈다. | ||
| + | |||
| + | <sxh javascript ; title: | ||
| + | import {Component} from ' | ||
| + | |||
| + | // 화면에 보이는 부분(Component) 정의 | ||
| + | // 만약 속성값이 여러줄이면 `(억음부호)을 사용한다. | ||
| + | @Component({ | ||
| + | selector: ' | ||
| + | templateUrl: | ||
| + | styleUrls: | ||
| + | }) | ||
| + | |||
| + | // Logic 정의 | ||
| + | // template에서 {{name}} 혹은 {{getName()}}으로 호출 | ||
| + | export class PersonComponent { | ||
| + | private name:string = " | ||
| + | |||
| + | public getName(): | ||
| + | return this.name; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | == Dependency Inject == | ||
| + | |||
| + | |||
| + | === 동적 Dependency Inject === | ||
| + | |||
| + | * @Host() : 상위 DI 정보를 찾지 않고, **현재 Component**에서 DI 정보를 찾아서 주입 | ||
| + | * @Optional() : DI 정보가 있으면 주입, 없으면 주입하지 않음. | ||
| + | |||
| + | <sxh javascript> | ||
| + | export class DynamicDependencyInectTestComponent { | ||
| + | |||
| + | constructor( | ||
| + | // 이 때, doctor은 상위 Component에서 이미 주입을 받았다고 가정. | ||
| + | // 결과적으로 @Host() 특성상 현재 Component만 기준으로 하므로 null 반환, @Optional로 인해 person에 programmer를 할당. | ||
| + | | ||
| + | @Host() | ||
| + | @Optional() | ||
| + | doctor: Person, | ||
| + | programmer: Person | ||
| + | ) { | ||
| + | this.person = doctor ? doctor : programmer; | ||
| + | } | ||
| + | | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | = Directive = | ||
| + | |||
| + | Template을 동적으로 만들어주는 요소. | ||
| + | == Structural directive == | ||
| + | DOM 요소 동적 처리 (ngIf, ngFor, ngSwitch, ...) | ||
| + | <sxh html> | ||
| + | <button type=" | ||
| + | |||
| + | <span *ngIf=" | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | |||
| + | == Attriube directive == | ||
| + | DOM 속성 동적 처리 (ngClass, ngStyle). Property Binding을 이용하여 변수값 하나에 대한 바인딩이 아닌 여러 값(객체)으로 Binding이 필요한 경우 사용. | ||
| + | |||
| + | <sxh html ; title: | ||
| + | <div> [ngClass]=" | ||
| + | </ | ||
| + | |||
| + | <sxh javascript ; title: | ||
| + | <div [ngStyle]=" | ||
| + | |||
| + | // component | ||
| + | myStyle = { | ||
| + | background-color: | ||
| + | visibility: this.isDisabled? | ||
| + | } | ||
| + | </ | ||
| + | == 영역 == | ||
| + | |||
| + | <sxh javascript ; title: | ||
| + | import {Directive, ElementRef, Renderer, HostListener} from ' | ||
| + | |||
| + | @Directive({ | ||
| + | selector:' | ||
| + | host: { | ||
| + | // Event 정의 | ||
| + | } | ||
| + | }) | ||
| + | |||
| + | export class MyColorDirective { | ||
| + | |||
| + | //속성 구현... | ||
| + | @Input(' | ||
| + | |||
| + | constructor(private el: | ||
| + | } | ||
| + | | ||
| + | // Event Listener 구현 | ||
| + | @HostListener(' | ||
| + | this.renderer.setElementStyle( | ||
| + | this.el.nativeElement, | ||
| + | ' | ||
| + | this.color); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | = Pipe = | ||
| + | Template에 Data를 보여줄 때 가공이 필요한 경우 사용. | ||
| + | <sxh html ; title: | ||
| + | {{ person | personPipe}} | ||
| + | </ | ||
| + | |||
| + | <sxh javascript ; title: | ||
| + | import { Pipe, PipeTransform } from ' | ||
| + | |||
| + | @Pipe({ | ||
| + | name: ' | ||
| + | }) | ||
| + | export class PersonPipe implements PipeTransform { | ||
| + | |||
| + | transform(person: | ||
| + | return `${person.name} (${person.age})`; | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | = Module = | ||
| + | |||
| + | Angular 안의 관련된 요소를 하나로 묶어 Application을 구성하는 단위. | ||
| + | |||
| + | <sxh javascript ; title: | ||
| + | import { NgModule } from ' | ||
| + | import { BrowserModule } from ' | ||
| + | import { AppComponent } from ' | ||
| + | |||
| + | @NgModule({ | ||
| + | imports: | ||
| + | providers: [Logger], // Service 등록 | ||
| + | declarations: | ||
| + | exports: [AppComponent], | ||
| + | bootstrap: | ||
| + | }) | ||
| + | |||
| + | export class AppModule { } | ||
| + | </ | ||
| + | |||
| + | == Bootstraping == | ||
| + | Application을 최초 실행할 때 진행되는 과정. (Compile 목적) | ||
| + | |||
| + | |||
| + | = Life Cycle = | ||
| + | https:// | ||
| + | |||
| + | == 수동으로 강제 변화 감지 == | ||
| + | * **ApplicationRef.tick()** : 전체 Component 검사 | ||
| + | * **[[https:// | ||
| + | * **ChangeDetectorRef.detectChanges()** : 자신과 자식 component만 검사. | ||
| + | |||
| + | = Trouble Shooting = | ||
| + | |||
| + | == 6+에서 polyfils.ts에 rx 연산자가 인식 안되는 경우 == | ||
| + | |||
| + | cli에서 설치하는 rxjs가 5.x이기 때문. | ||
| + | |||
| + | <sxh bash> | ||
| + | npm install rxjs@6 rxjs-compat@6 --save | ||
| + | </ | ||
| + | |||
| + | |||
| + | == Build 이후 Path로 인한 SecurityError == | ||
| + | |||
| + | https:// | ||
| + | |||
| + | |||
| + | * RouterModule에서 useHash 설정 | ||
| + | |||
| + | <sxh javascript> | ||
| + | RouterModule.forRoot(routes, | ||
| + | </ | ||
| + | |||
| + | |||
| + | * index.html 수정 | ||
| + | |||
| + | <sxh html> | ||
| + | <!-- <base href="/"> | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | |||
| + | * And then build with --base-href | ||
| + | |||
| + | <sxh bash> | ||
| + | ng build --prod --base-href ./ | ||
| + | </ | ||
| + | |||
| == Creating libraries | == Creating libraries | ||