リリース・改善中
Angular ガイド · 1/6
この章は現在、英語でのみ提供しています。
This chapter walks through installing the Angular CLI, creating a project, running the development server and making a first change. It finishes with a few editor tools that make day-to-day Angular work much smoother.
Angular projects are built and served by the Angular CLI, which runs on Node.js. Install an active LTS or maintenance LTS release of Node.js from nodejs.org; each Angular release lists the Node.js versions it supports in the version compatibility table on angular.dev. npm comes with Node.js, but pnpm, Yarn and Bun also work.
node --version
npm --versionInstall the CLI globally so the ng command is available everywhere:
npm install -g @angular/cli
ng versionOn Windows, PowerShell may refuse to run the ng script because of its execution policy. The Angular docs suggest allowing locally created scripts for your user with Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned. If you prefer not to install anything globally, you can run the CLI through npx instead, for example npx @angular/cli new my-app.
ng new creates a workspace folder, installs dependencies and initializes a Git repository:
ng new my-appThe CLI asks a few questions, such as which stylesheet format to use (CSS, SCSS, Sass or Less) and whether to enable server-side rendering (SSR) and static site generation. You can answer them up front with flags:
| Option | What it does |
|---|---|
--style=scss | Use SCSS for component and global styles |
--ssr | Add server-side rendering and prerendering support |
--skip-tests | Do not generate .spec.ts test files |
--skip-git | Do not initialize a Git repository |
--package-manager=pnpm | Install dependencies with another package manager |
--defaults | Accept the default answer for every prompt |
cd my-app
ng serve --openThe app is served at http://localhost:4200. The dev server watches your files, rebuilds incrementally and reloads the browser when you save. Use to pick another port. The generated also maps to , so both commands do the same thing.
ng serve --port 4300package.jsonnpm startng serveThe root component lives in src/app. Recent CLI versions name the files app.ts, app.html and app.css with a class called App; projects created with older versions use app.component.ts and AppComponent. The idea is the same either way:
import { Component, signal } from '@angular/core';
import { RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
imports: [RouterOutlet],
templateUrl: './app.html',
styleUrl: './app.css',
})
export class App {
protected readonly title = signal('my-app');
}Replace the contents of the template with something small and save the file:
<h1>Hello, {{ title() }}!</h1>
<p>Edit src/app/app.html and save to see the change.</p>
<router-outlet />{{ title() }} reads the title signal and renders its value. <router-outlet /> is where routed pages will appear once you add routes.
The CLI generates components, services and other building blocks with consistent names and a matching test file:
ng generate component hero-list
ng g service hero
ng g guard auth
ng generate --helpng g is short for ng generate. Add --dry-run to preview which files would be created. ng add installs a library and runs its setup schematic, for example ng add @angular/material.
npm install -g @angular/cli.ng new creates a project and ng serve runs it at http://localhost:4200 with live reload.src/app; templates read signals by calling them.ng generate for new code and the Angular Language Service in your editor.
コメント 0件
ログイン · ログインするとコメントできます。
最初のコメントを書いてみましょう。