npm install -g @angular/cli
ng help
ng new
CLI 를 통해 Angular 프로젝트를 생성할 수 있다.ng new <project-name>
ng serve
명령어를 통해 프로젝트를 실행할 수 있다.ng serve
명령은 서버를 시작하고, 파일을 감시할 뿐만 아니라 해당 파일을 변경할 때 앱을 다시 빌드하고 브라우저를 다시 로드한다.--open
옵션은 앱을 빌드하며 브라우저를 자동으로 실행해준다.cd <project-name>
ng serve --open
http://localhost:4200/
을 입력하여 Angular 개발용 서버에 접속할 수 있다.ng generate
명령어를 사용한다.ng generate
명령어는 축약형 ng g
와 동일하게 동작한다.생성 대상 구성요소 | 명령어 | 축약형 |
---|---|---|
컴포넌트 | ng generate component component-name | ng g c component-name |
디렉티브 | ng generate directive directive-name | ng g d directive-name |
파이프 | ng generate pipe pipe-name | ng g p pipe-name |
서비스 | ng generate service service-name | ng g s service-name |
모듈 | ng generate module module-name | ng g m module-name |
가드 | ng generate guard guard-name | ng g g guard-name |
클래스 | ng generate class class-name | ng g cl class-name |
인터페이스 | ng generate interface interface-name | ng g i interface-name |
Enum | ng generate enum enum-name | ng g e enum-name |
ng generate component home
명령어를 실행하면 Angular CLI는 아래와 같이 동작한다.
src/app
폴더에 home
폴더를 생성한다.src/app/home
폴더에 4개의 파일을 생성한다.home.component.html
컴포넌트 템플릿을 위한 HTML 파일home.component.css
컴포넌트 템플릿의 스타일링을 위한 CSS 파일home.component.ts
컴포넌트 클래스 파일home.component.spec.ts
컴포넌트 유닛 테스트를 위한 스펙 파일--type
옵션을 사용하면 component를 page 타입으로 만들 수도 있다.ng generate component home --type=page
src/app/home/home.component.ts
을 살펴보자.import { Component } from '@angular/core';
@Component({
selector: 'app-home',
standalone: true,
imports: [],
templateUrl: './home.component.html',
styleUrl: './home.component.scss'
})
export class HomeComponent {
}
selector
프로퍼티에 ‘app-home’이 설정되어 있다.selector
프로퍼티는 컴포넌트를 마크업으로 표현할 때 사용하는 이름이다.src/app/app.component.ts
에서 home
컴포넌트를 사용하려면 아래와 같이 작성하면 된다.<!-- src/app/app.component.html -->
<app-home></app-home>
selector
프로퍼티의 기본 접두사는 app 이며 angular.json
에서 확인할 수 있다.{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"my-app": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app", // 기본 접두사
...
}
angular.json
의 prefix 프로퍼티값을 수정하면 이후 생성되는 컴포넌트의 셀렉터 접두사는 수정된 값으로 변경된다.ng new
명령어로 프로젝트를 생성할 때 --prefix
옵션을 추가한다.ng new my-app --prefix <prefix-name>
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init
tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ["./src/**/*.{html,ts}"],
theme: {
extend: {},
},
plugins: [],
};
@tailwind
지시문 추가./src/styles.css
@tailwind base;
@tailwind components;
@tailwind utilities;
<h1 class="text-3xl font-bold underline">Hello world!</h1>
References