ionic-angular
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseIonic Angular
Ionic Angular
Ionic development with Angular — project structure, Angular-specific components, navigation, lazy loading, forms, and lifecycle integration.
基于Angular的Ionic开发 —— 包括项目结构、Angular专属组件、导航、懒加载、表单和生命周期集成。
Prerequisites
前置条件
- Ionic Framework 7 or 8 with .
@ionic/angular - Angular 16+ (Angular 17+ recommended for standalone architecture).
- Node.js and npm installed.
- Ionic CLI installed ().
npm install -g @ionic/cli
- Ionic Framework 7或8 搭配。
@ionic/angular - Angular 16+(独立组件架构推荐使用Angular 17+)。
- 已安装Node.js和npm。
- 已安装Ionic CLI()。
npm install -g @ionic/cli
Agent Behavior
Agent行为规范
- Auto-detect architecture. Check for
src/main.ts(standalone) vs.bootstrapApplication(NgModule). Adapt all code examples to the detected architecture.platformBrowserDynamic().bootstrapModule - Guide step-by-step. Walk the user through one topic at a time.
- Adapt imports. For standalone components, import each Ionic component from . For NgModule components, import
@ionic/angular/standalonein the page module.IonicModule
- 自动检测架构:检查中是
src/main.ts(独立组件模式)还是bootstrapApplication(NgModule模式)。所有代码示例都要适配检测到的架构。platformBrowserDynamic().bootstrapModule - 分步指导:一次只引导用户完成一个主题。
- 适配导入逻辑:对于独立组件,从导入每个Ionic组件;对于NgModule组件,在页面模块中导入
@ionic/angular/standalone。IonicModule
Procedures
操作流程
Step 1: Analyze the Project
步骤1:分析项目
Auto-detect the following by reading project files — do not ask the user for information that can be inferred:
- Ionic version: Read version from
@ionic/angular.package.json - Angular version: Read version from
@angular/core.package.json - Architecture: Check for
src/main.ts(standalone) orbootstrapApplication(NgModule). IfplatformBrowserDynamic().bootstrapModuleexists and importssrc/app/app.module.ts, confirm NgModule.IonicModule - Capacitor: Check if is in
@capacitor/core. Check forpackage.jsonandandroid/directories.ios/ - Routing: Check for (standalone) or
src/app/app.routes.ts(NgModule).src/app/app-routing.module.ts - Navigation pattern: Check for a
src/app/directory (tab-based) ortabs/usage in templates (side menu).ion-menu
通过读取项目文件自动检测以下内容——不要向用户询问可以推断出的信息:
- Ionic版本:从中读取
package.json的版本。@ionic/angular - Angular版本:从中读取
package.json的版本。@angular/core - 架构类型:检查中的
src/main.ts(独立组件)或bootstrapApplication(NgModule)。如果platformBrowserDynamic().bootstrapModule存在且导入了src/app/app.module.ts,则确认为NgModule架构。IonicModule - Capacitor配置:检查中是否包含
package.json,检查是否存在@capacitor/core和android/目录。ios/ - 路由配置:检查是否存在(独立组件模式)或
src/app/app.routes.ts(NgModule模式)。src/app/app-routing.module.ts - 导航模式:检查下是否有
src/app/目录(基于标签页的导航),或者模板中是否使用了tabs/(侧边菜单导航)。ion-menu
Step 2: Understand Project Structure
步骤2:了解项目结构
Read for the standard Ionic Angular directory layout.
references/project-structure.mdKey points:
- Standalone projects have ,
app.config.ts, and standalone page components.app.routes.ts - NgModule projects have ,
app.module.ts, and per-pageapp-routing.module.tsfiles.*.module.ts - Ionic config lives in .
ionic.config.json - Theme variables are in .
src/theme/variables.scss - Global styles and Ionic CSS imports are in .
src/global.scss
阅读了解标准Ionic Angular目录结构。
references/project-structure.md关键要点:
- 独立组件项目包含、
app.config.ts和独立页面组件。app.routes.ts - NgModule项目包含、
app.module.ts,以及每个页面对应的app-routing.module.ts文件。*.module.ts - Ionic配置存放在中。
ionic.config.json - 主题变量存放在中。
src/theme/variables.scss - 全局样式和Ionic CSS导入存放在中。
src/global.scss
Step 3: Understand Architecture Differences
步骤3:了解架构差异
Read for detailed differences.
references/standalone-vs-ngmodules.mdSummary:
| Aspect | Standalone | NgModule |
|---|---|---|
| Bootstrap | | |
| Ionic setup | | |
| Component imports | Import each Ionic component per file | |
| Import source | | |
| Lazy loading | | |
| Icon registration | | Automatic |
| Tree-shaking | Yes | No |
阅读了解详细差异。
references/standalone-vs-ngmodules.md摘要:
| 维度 | 独立组件(Standalone) | NgModule |
|---|---|---|
| 引导方式 | | |
| Ionic配置 | | |
| 组件导入 | 每个文件单独导入所需Ionic组件 | |
| 导入来源 | | |
| 懒加载 | 路由中使用 | 路由中使用 |
| 图标注册 | 需要调用 | 自动注册 |
| Tree-shaking | 支持 | 不支持 |
Step 4: Set Up Navigation
步骤4:配置导航
Read for full navigation patterns.
references/navigation.mdTopics covered:
- Route configuration — defining routes with lazy loading for both architectures.
- Root component layout — with
<ion-app>.<ion-router-outlet> - Template navigation — with
routerLinkfor transition animations.routerDirection - Programmatic navigation — (
NavController,navigateForward,navigateBack,navigateRoot).back - Route parameters — reading params via .
ActivatedRoute - Tab navigation — with child routes per tab.
<ion-tabs> - Side menu — with
<ion-menu>and child routes.<ion-menu-toggle> - Route guards — functional guards (Angular 15.2+) with .
CanActivateFn - Modals — for overlay navigation with data passing.
ModalController - Linear vs. non-linear routing — when to use each pattern.
阅读了解完整导航模式。
references/navigation.md涵盖的主题:
- 路由配置——为两种架构定义支持懒加载的路由。
- 根组件布局——包含的
<ion-router-outlet>。<ion-app> - 模板内导航——搭配实现转场动画的
routerDirection。routerLink - 编程式导航——(
NavController、navigateForward、navigateBack、navigateRoot)。back - 路由参数——通过读取参数。
ActivatedRoute - 标签页导航——每个标签页对应子路由的。
<ion-tabs> - 侧边菜单——搭配和子路由的
<ion-menu-toggle>。<ion-menu> - 路由守卫——Angular 15.2+版本中使用的函数式守卫。
CanActivateFn - 模态框——用于覆盖层导航并支持数据传递的。
ModalController - 线性与非线性路由——各自的适用场景。
Step 5: Implement Lifecycle Hooks
步骤5:实现生命周期钩子
Read for Ionic page lifecycle hook details.
references/lifecycle.mdKey rules:
- fires once (first creation). Use for one-time setup.
ngOnInit - fires on every page visit. Use for refreshing data.
ionViewWillEnter - fires after the transition animation. Use for heavy work that would block animations.
ionViewDidEnter - fires before leaving. Use for cleanup, pausing subscriptions.
ionViewWillLeave - fires when the page is popped from the navigation stack. Use for final cleanup.
ngOnDestroy - Implement interfaces ,
ViewWillEnter,ViewDidEnter,ViewWillLeavefromViewDidLeave.@ionic/angular
阅读了解Ionic页面生命周期钩子的详细信息。
references/lifecycle.md关键规则:
- 仅触发一次(页面首次创建时),用于一次性初始化操作。
ngOnInit - 在每次进入页面时触发,用于刷新数据。
ionViewWillEnter - 在转场动画完成后触发,用于执行会阻塞动画的重型操作。
ionViewDidEnter - 在离开页面前触发,用于清理、暂停订阅。
ionViewWillLeave - 在页面从导航栈中弹出时触发,用于最终清理。
ngOnDestroy - 实现导出的
@ionic/angular、ViewWillEnter、ViewDidEnter、ViewWillLeave接口。ViewDidLeave
Step 6: Build Forms
步骤6:构建表单
Read for form patterns with Ionic components.
references/forms.mdTopics covered:
- Reactive forms — ,
FormBuilder,FormGroupwith Ionic input components.Validators - Template-driven forms — with
[(ngModel)].FormsModule - Ionic form components — ,
ion-input,ion-textarea,ion-select,ion-checkbox,ion-toggle,ion-radio-group,ion-range.ion-datetime - Validation display — (Ionic 7+) or manual
errorTextfor older versions.<ion-note> - Label placement — attribute (
labelPlacement,floating,stacked).fixed
阅读了解结合Ionic组件的表单模式。
references/forms.md涵盖的主题:
- 响应式表单——搭配Ionic输入组件使用、
FormBuilder、FormGroup。Validators - 模板驱动表单——搭配使用
FormsModule。[(ngModel)] - Ionic表单组件——、
ion-input、ion-textarea、ion-select、ion-checkbox、ion-toggle、ion-radio-group、ion-range。ion-datetime - 校验提示展示——Ionic 7+使用,旧版本手动使用
errorText。<ion-note> - 标签位置——属性(
labelPlacement浮动、floating堆叠、stacked固定)。fixed
Step 7: Manage Services and State
步骤7:管理服务与状态
Read for service and state management patterns.
references/services-and-state.mdTopics covered:
- Injectable services — .
@Injectable({ providedIn: 'root' }) - HTTP data fetching — setup for standalone and NgModule, CRUD service pattern.
HttpClient - Reactive state — for shared observable state.
BehaviorSubject - Angular signals — and
signal()for reactive state (Angular 16+).computed() - Loading indicators — for async operations.
LoadingController - Error feedback — for user-facing error/success messages.
ToastController - Environment config — /
environment.tsfor API URLs.environment.prod.ts
阅读了解服务与状态管理模式。
references/services-and-state.md涵盖的主题:
- 可注入服务——。
@Injectable({ providedIn: 'root' }) - HTTP数据获取——适配独立组件和NgModule的配置,CRUD服务模式。
HttpClient - 响应式状态——用于共享可观察状态的。
BehaviorSubject - Angular signals——Angular 16+中用于响应式状态的和
signal()。computed() - 加载指示器——用于异步操作的。
LoadingController - 错误反馈——向用户展示错误/成功消息的。
ToastController - 环境配置——存放API地址的/
environment.ts。environment.prod.ts
Step 8: Optimize Performance
步骤8:优化性能
Read for performance optimization techniques.
references/performance.mdTopics covered:
- Lazy loading — (standalone) and
loadComponent(NgModule).loadChildren - trackBy / track — efficient list rendering with and
*ngFor.@for - Virtual scrolling — Angular CDK for large lists.
cdk-virtual-scroll-viewport - OnPush change detection — reduce unnecessary re-renders.
- Preloading strategy — for instant subsequent navigation.
PreloadAllModules - Skeleton text — for perceived performance during loading.
ion-skeleton-text
阅读了解性能优化技巧。
references/performance.md涵盖的主题:
- 懒加载——独立组件使用,NgModule使用
loadComponent。loadChildren - trackBy / track——搭配和
*ngFor实现高效列表渲染。@for - 虚拟滚动——用于长列表的Angular CDK 。
cdk-virtual-scroll-viewport - OnPush变更检测——减少不必要的重渲染。
- 预加载策略——实现后续导航秒开的。
PreloadAllModules - 骨架屏——提升加载过程中感知性能的。
ion-skeleton-text
Step 9: Write Tests
步骤9:编写测试
Read for testing patterns.
references/testing.mdTopics covered:
- Unit testing pages — setup for standalone and NgModule pages.
TestBed - Testing services — basic services and HTTP services with .
HttpTestingController - Mocking dependencies — for service and controller mocks.
jasmine.createSpyObj - Testing navigation — mocking .
NavController - Testing guards — for functional guards.
TestBed.runInInjectionContext - E2E testing — Cypress and Playwright setup.
阅读了解测试模式。
references/testing.md涵盖的主题:
- 页面单元测试——适配独立组件和NgModule页面的配置。
TestBed - 服务测试——基础服务测试,以及使用测试HTTP服务。
HttpTestingController - 依赖Mock——使用模拟服务和控制器。
jasmine.createSpyObj - 导航测试——模拟。
NavController - 守卫测试——针对函数式守卫的。
TestBed.runInInjectionContext - E2E测试——Cypress和Playwright配置。
Error Handling
错误处理
- : In standalone apps, verify
NullInjectorError: No provider for IonRouterOutletis called inprovideIonicAngular({}). In NgModule apps, verifyapp.config.tsis imported inIonicModule.forRoot().app.module.ts - Ionic components not rendering: In standalone components, verify each Ionic component is imported from and listed in the
@ionic/angular/standalonearray. In NgModule components, verifyimportsis imported in the page module.IonicModule - Icons not showing (standalone): Verify is called with the required icons from
addIcons(), andionicons/iconsis imported fromIonIcon.@ionic/angular/standalone - not firing: Verify the component is directly routed via
ionViewWillEnter. Child components do not receive Ionic lifecycle events.<ion-router-outlet> - Page data not refreshing on back navigation: Use instead of
ionViewWillEnterfor data loading. Ionic caches pages in the DOM, songOnInitonly fires once.ngOnInit - Form validation errors not displaying: For Ionic 7+, use on
errorText. For older versions, manually check<ion-input>and showcontrol.invalid && control.touched.<ion-note> - not working on Ionic components: In standalone components, import
routerLinkfromRouterLinkin the component's@angular/routerarray.imports - Page transitions not animating: Use from
NavControllerinstead of Angular's@ionic/angularfor animated navigation. Alternatively, addRoutertorouterDirectionelements.routerLink - Tab navigation broken: Verify the attribute on
tabmatches the child route<ion-tab-button>. Do not programmatically navigate between tabs.path
- :独立组件应用中,请确认
NullInjectorError: No provider for IonRouterOutlet中调用了app.config.ts;NgModule应用中,请确认provideIonicAngular({})中导入了app.module.ts。IonicModule.forRoot() - Ionic组件不渲染:独立组件中,请确认每个Ionic组件都从导入并添加到
@ionic/angular/standalone数组中;NgModule组件中,请确认页面模块中导入了imports。IonicModule - 图标不显示(独立组件模式):请确认调用了引入
addIcons()中的所需图标,且已从ionicons/icons导入了@ionic/angular/standalone。IonIcon - 不触发:请确认组件是通过
ionViewWillEnter直接路由的,子组件不会收到Ionic生命周期事件。<ion-router-outlet> - 返回导航时页面数据不刷新:数据加载请使用而非
ionViewWillEnter,Ionic会在DOM中缓存页面,因此ngOnInit仅会触发一次。ngOnInit - 表单校验错误不展示:Ionic 7+请在上使用
<ion-input>;旧版本请手动检查errorText并展示control.invalid && control.touched。<ion-note> - Ionic组件上的不生效:独立组件中,请在组件的
routerLink数组中从imports导入@angular/router。RouterLink - 页面转场无动画:请使用导出的
@ionic/angular而非Angular原生NavController实现带动画的导航,也可以在Router元素上添加routerLink属性。routerDirection - 标签页导航失效:请确认上的
<ion-tab-button>属性与子路由的tab匹配,不要通过编程方式在标签页之间跳转。path
Related Skills
相关技能
- — Capacitor-specific Angular patterns (plugin services, NgZone, deep links, platform detection).
capacitor-angular - — General Ionic development (components, theming, CLI).
ionic-app-development - — Create a new Ionic app from scratch.
ionic-app-creation - — Upgrade Ionic to a newer version.
ionic-app-upgrades
- ——Capacitor专属的Angular模式(插件服务、NgZone、深度链接、平台检测)。
capacitor-angular - ——通用Ionic开发(组件、主题、CLI)。
ionic-app-development - ——从零创建新Ionic应用。
ionic-app-creation - ——升级Ionic到新版本。
ionic-app-upgrades