准备工作
确保您已经完成了前一章的环境设置:
- Node.js (v16.x 或 v18.x) 已安装
- npm (8.x 或更高版本) 已安装
- Angular CLI 已全局安装
- 代码编辑器(推荐VS Code)已准备就绪
步骤1:创建新项目
打开终端或命令提示符,使用ng new命令创建新项目:
CLI会询问一些配置选项,这里我们详细解释每个选项:
Angular路由
输入y启用路由功能。Angular会为你的应用生成一个基本的路由模块,这对于创建多页面单页应用非常重要。
样式表格式
使用方向键选择样式格式:
- CSS - 标准的CSS(推荐初学者)
- SCSS - Sass的SCSS语法(功能更强大)
- Sass - 缩进语法版的Sass
- Less - Less预处理器
SSR/SSG选项
输入y启用服务器端渲染和静态站点生成。这对于SEO优化和首屏加载性能非常重要,但对于第一个应用可以先选N。
步骤2:项目结构解析
项目创建完成后,让我们看看生成的文件结构:
核心文件详细说明
angular.json
Angular CLI的工作区配置文件,包含构建、测试、打包等所有配置选项。
package.json
定义项目依赖、npm脚本和项目元数据。包含了所有第三方库的版本信息。
app.module.ts
应用的根模块,声明了应用中使用的组件、指令、管道和服务。
main.ts
应用的入口点,负责启动Angular应用,将根组件挂载到DOM中。
步骤3:理解根组件
让我们查看生成的根组件代码,这是Angular应用的核心:
src/app/app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'my-first-angular-app';
}
组件装饰器解析
- @Component - 这是一个装饰器函数,它将TypeScript类标记为Angular组件
- selector: 'app-root' - 定义组件在HTML中的标签名,在其他模板中使用<app-root></app-root>
- templateUrl - 指定组件模板文件的位置
- styleUrls - 指定组件样式文件的位置(数组,可以多个)
组件类解析
- export class AppComponent - 导出AppComponent类,这是组件的控制器
- title = 'my-first-angular-app' - 组件的属性,可以在模板中使用
src/app/app.component.html
<div class="content" role="main">
<div class="card highlight-card card-small">
<span>{{ title }} app is running!</span>
</div>
<h2>Resources</h2>
<p>Here are some links to help you get started:</p>
<div class="card-container">
<a class="card" target="_blank" rel="noopener" href="https://angular.io/tutorial">
<svg>...</svg>
<span>Learn Angular</span>
<p>Learn Angular with interactive tutorials</p>
</a>
<a class="card" target="_blank" rel="noopener" href="https://angular.io/cli">
<svg>...</svg>
<span>CLI Documentation</span>
<p>Angular CLI documentation and command reference</p>
</a>
<a class="card" target="_blank" rel="noopener" href="https://blog.angular.io/">
<svg>...</svg>
<span>Angular Blog</span>
<p>News and tips from the Angular team</p>
</a>
</div>
</div>
注意{{ title }},这是Angular的数据绑定语法,它会将组件中的title属性值渲染到模板中。
步骤4:运行应用
进入项目目录并启动开发服务器:
ng serve- 启动开发服务器--open- 自动在默认浏览器中打开应用(可简写为-o)--port 4200- 可以指定端口(默认4200)--host 0.0.0.0- 允许网络访问
实时预览
Resources
Here are some links to help you get started:
Learn Angular
Learn Angular with interactive tutorials
CLI Documentation
Angular CLI documentation and command reference
Angular Blog
News and tips from the Angular team
步骤5:修改应用(创建你的第一个组件)
现在让我们创建一个自定义组件,体验Angular的开发流程:
或者使用简写:
welcome.component.ts- 组件逻辑welcome.component.html- 组件模板welcome.component.css- 组件样式welcome.component.spec.ts- 组件测试文件
修改Welcome组件
src/app/welcome/welcome.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-welcome',
templateUrl: './welcome.component.html',
styleUrls: ['./welcome.component.css']
})
export class WelcomeComponent {
currentDate = new Date();
visitorName = 'Angular开发者';
visitorCount = 0;
incrementCount() {
this.visitorCount++;
}
getGreeting(): string {
const hour = this.currentDate.getHours();
if (hour < 12) return '早上好';
if (hour < 18) return '下午好';
return '晚上好';
}
}
src/app/welcome/welcome.component.html
<div class="welcome-container">
<h1>{{ getGreeting() }}, {{ visitorName }}!</h1>
<p>欢迎来到你的第一个Angular应用</p>
<div class="info-card">
<p>当前时间: {{ currentDate | date:'yyyy-MM-dd HH:mm:ss' }}</p>
<p>访问次数: {{ visitorCount }}</p>
</div>
<button class="btn btn-primary" (click)="incrementCount()">
增加访问计数
</button>
<div *ngIf="visitorCount > 5" class="alert alert-success mt-3">
恭喜!你已经多次访问这个组件了!
</div>
</div>
在根组件中使用Welcome组件
修改app.component.html,添加我们创建的组件:
<!-- 在app.component.html中添加 -->
<div class="content" role="main">
<!-- 原始内容... -->
<!-- 添加我们的Welcome组件 -->
<app-welcome></app-welcome>
</div>
步骤6:理解Angular模块
查看app.module.ts文件,理解Angular模块系统:
src/app/app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { WelcomeComponent } from './welcome/welcome.component';
@NgModule({
declarations: [
AppComponent,
WelcomeComponent
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
NgModule装饰器解析
- declarations - 声明属于这个模块的组件、指令和管道
- imports - 导入其他模块(如BrowserModule、FormsModule等)
- providers - 注册服务,这些服务在整个模块中可用
- bootstrap - 指定启动哪个组件作为根组件
模块化的重要性
Angular的模块系统帮助组织代码,实现:
- 封装性 - 相关功能组织在一起
- 复用性 - 模块可以在不同项目中复用
- 懒加载 - 提高应用启动速度
- 清晰的依赖管理 - 明确组件间的依赖关系
常用CLI命令总结
| 命令 | 描述 | 简写 |
|---|---|---|
ng new project-name |
创建新的Angular项目 | - |
ng serve |
启动开发服务器 | - |
ng generate component |
生成新组件 | ng g c |
ng generate service |
生成新服务 | ng g s |
ng generate module |
生成新模块 | ng g m |
ng build |
构建生产版本 | - |
ng test |
运行单元测试 | - |
ng lint |
运行代码检查 | - |
恭喜!第一个Angular应用创建完成!
你已经成功创建并运行了第一个Angular应用,了解了项目结构、组件创建和模块系统。接下来可以继续深入学习Angular的核心概念。