Ionic Angular 快速入门
欢迎!本指南将带你了解 Ionic Angular 开发的基础知识。你将学习如何设置开发环境、生成一个简单的项目、探索项目结构,并理解 Ionic 组件的工作原理。这对于在构建第一个真实应用之前熟悉 Ionic Angular 来说是完美的。
如果你需要了解 Ionic Angular 是什么以及它在 Angular 生态系统中的位置,请查看 Ionic Angular 概览。
前提条件
开始之前,请确保你的机器上已安装 Node.js 和 npm。 你可以通过运行以下命令来检查:
node -v
npm -v
如果你没有安装 Node.js 和 npm,请 在此处下载 Node.js(它包含了 npm)。
使用 Ionic CLI 创建项目
首先,安装最新的 Ionic CLI:
npm install -g @ionic/cli
然后,运行以下命令来创建并运行一个新项目:
ionic start myApp blank --type angular
cd myApp
ionic serve
在第一个提示处,选择 Standalone。
运行 ionic serve 后,你的项目将在浏览器中打开。

探索项目结构
你的新应用目录结构如下所示:
└── src/
└── app
├── app.component.html
├── app.component.scss
├── app.component.ts
├── app.routes.ts
└── home/
├── home.page.html
├── home.page.scss
├── home.page.spec.ts
└── home.page.ts
以下示例中的所有文件路径都是相对于项目根目录的。
让我们逐一查看这些文件,了解应用的结构。
查看应用根组件
应用的根定义在 app.component.ts 中:
import { Component } from '@angular/core';
import { IonApp, IonRouterOutlet } from '@ionic/angular/standalone';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
imports: [IonApp, IonRouterOutlet],
})
export class AppComponent {
constructor() {}
}
其模板在 app.component.html 中:
<ion-app>
<ion-router-outlet></ion-router-outlet>
</ion-app>
这设置了应用的根,使用了 Ionic 的 ion-app 和 ion-router-outlet 组件。路由出口是显示页面的地方。
查看路由定义
路由在 app.routes.ts 中定义:
import { Routes } from '@angular/router';
export const routes: Routes = [
{
path: 'home',
loadComponent: () => import('./home/home.page').then((m) => m.HomePage),
},
{
path: '',
redirectTo: 'home',
pathMatch: 'full',
},
];
当你访问根 URL (/) 时,将会加载 HomePage 组件。
查看主页组件
主页组件定义在 home.page.ts 中,并导入了它使用的 Ionic 组件:
import { Component } from '@angular/core';
import { IonHeader, IonToolbar, IonTitle, IonContent } from '@ionic/angular/standalone';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
imports: [IonHeader, IonToolbar, IonTitle, IonContent],
})
export class HomePage {
constructor() {}
}
模板文件 home.page.html 使用了这些组件:
<ion-header [translucent]="true">
<ion-toolbar>
<ion-title> Blank </ion-title>
</ion-toolbar>
</ion-header>
<ion-content [fullscreen]="true">
<ion-header collapse="condense">
<ion-toolbar>
<ion-title size="large">Blank</ion-title>
</ion-toolbar>
</ion-header>
<div id="container">
<strong>准备好创建应用了吗?</strong>
<p>
从 Ionic
<a target="_blank" rel="noopener noreferrer" href="https://ionicframework.com/docs/components">UI 组件</a> 开始
</p>
</div>
</ion-content>
这创建了一个带有标题和可滚动内容区域的页面。第二个标题显示了一个 可折叠大标题,当位于内容顶部时显示,向下滚动时则会收起,在第一个标题中显示较小的标题。
添加 Ionic 组件
你可以使用更多 Ionic UI 组件来增强你的主页。例如,在 ion-content 的末尾添加一 个 Button:
<ion-content>
<!-- 现有内容 -->
<ion-button>导航</ion-button>
</ion-content>
然后,在 home.page.ts 中导入 IonButton 组件:
import { IonButton, IonContent, IonHeader, IonTitle, IonToolbar } from '@ionic/angular/standalone';
@Component({
// ...现有配置...
imports: [IonButton, IonContent, IonHeader, IonTitle, IonToolbar],
})
添加新页面
要添加新页面,请使用 CLI 生成:
ionic generate page new
路由将自动添加到 app.routes.ts。
在 new.page.html 中,你可以向 Toolbar 添加一个 返回按钮:
<ion-header [translucent]="true">
<ion-toolbar>
<ion-buttons slot="start">
<ion-back-button defaultHref="/"></ion-back-button>
</ion-buttons>
<ion-title>new</ion-title>
</ion-toolbar>
</ion-header>
并在 new.page.ts 中导入 IonBackButton 和 IonButtons:
import { IonBackButton, IonButtons, IonContent, IonHeader, IonTitle, IonToolbar } from '@ionic/angular/standalone';
@Component({
// ...现有配置...
imports: [IonBackButton, IonButtons, IonContent, IonHeader, IonTitle, IonToolbar],
})
ion-back-button 将自动处理导航回上一页,如果没有历史记录则导航到 /。
导航到新页面
要导航到新页面,更新 home.page.html 中的按钮:
<ion-button [routerLink]="['/new']">导航</ion-button>
然后,在 home.page.ts 中导入 RouterLink:
import { RouterLink } from '@angular/router';
@Component({
// ...现有配置...
imports: [IonButton, IonContent, IonHeader, IonTitle, IonToolbar, RouterLink],
})
导航也可以使用 Angular 的 Router 服务来执行。更多信息请参阅 Angular 导航文档。
向新页面添加图标
Ionic Angular 预装了 Ionicons。你可以通过设置 ion-icon 组件的 name 属性来使用任何图标。将以下图标添加到 new.page.html:
<ion-content>
<!-- 现有内容 -->
<ion-icon name="heart"></ion-icon>
<ion-icon name="logo-ionic"></ion-icon>
</ion-content>