雷达发射机:功率、频率与相参性的三维博弈
2026/6/9 17:23:00
鸿蒙应用交互设计:实现流畅的页面跳转与状态管理
✅学习目标
💡重点内容
AbilitySlice 生命周期、页面跳转方式、状态管理机制、数据传递策略
⚠️前置基础
已掌握鸿蒙 ArkTS 组件化开发、布局系统核心知识
鸿蒙应用的页面管理基于「Ability + AbilitySlice」架构:
PageAbility(用于界面展示)和ServiceAbility(用于后台服务)理解生命周期是实现流畅交互的基础,核心生命周期方法包括:
onStart():页面初始化时调用,仅执行一次onActive():页面进入前台时调用,可响应用户交互onForeground():页面从后台进入前台前调用onBackground():页面从前台进入后台前调用onStop():页面销毁时调用,释放资源PageAbility命名为MainAbilityAbilitySlice:MainSlice(首页)和DetailSlice(详情页)// MainSlice.ets - 首页 import { router } from '@ohos.router'; @Entry @Component struct MainSlice { build() { Column({ space: 20 }) { Text('首页') .fontSize(24) .fontWeight(FontWeight.Bold); // 跳转按钮 Button('跳转到详情页') .width(200) .height(40) .backgroundColor('#007DFF') .fontColor(Color.White) .onClick(() => { // 本地页面跳转 router.pushUrl({ url: 'pages/DetailSlice' }, router.RouterMode.Single); }); } .width('100%') .height('100%') .justifyContent(FlexAlign.Center); } } // DetailSlice.ets - 详情页 import { router } from '@ohos.router'; @Entry @Component struct DetailSlice { build() { Column({ space: 20 }) { Text('详情页') .fontSize(24) .fontWeight(FontWeight.Bold); // 返回按钮 Button('返回首页') .width(200) .height(40) .backgroundColor('#007DFF') .fontColor(Color.White) .onClick(() => { // 返回上一页 router.back(); }); } .width('100%') .height('100%') .justifyContent(FlexAlign.Center); } }PageAbility命名为SecondAbilitySecondSlice// MainSlice.ets - 首页 import { wantAgent } from '@ohos.app.ability.wantAgent'; import { common } from '@kit.AbilityKit'; @Entry @Component struct MainSlice { // 创建 WantAgent 用于跨 Ability 跳转 private wantAgent: wantAgent.WantAgent | null = null; async onPageShow() { // 构建跳转 Want const want: common.Want = { bundleName: 'com.example.myapp', abilityName: 'com.example.myapp.SecondAbility' }; // 构建 WantAgent 信息 const wantAgentInfo: wantAgent.WantAgentInfo = { wants: [want], operationType: wantAgent.OperationType.START_ABILITY, requestCode: 0, wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] }; // 获取 WantAgent this.wantAgent = await wantAgent.getWantAgent(wantAgentInfo); } build() { Column({ space: 20 }) { Text('首页') .fontSize(24) .fontWeight(FontWeight.Bold); // 跨 Ability 跳转按钮 Button('跳转到第二个 Ability') .width(240) .height(40) .backgroundColor('#007DFF') .fontColor(Color.White) .onClick(() => { if (this.wantAgent) { wantAgent.triggerWantAgent(this.wantAgent); } }); } .width('100%') .height('100%') .justifyContent(FlexAlign.Center); } }// MainSlice.ets - 首页 import { router } from '@ohos.router'; @Entry @Component struct MainSlice { private appInfo = { name: '鸿蒙天气', rating: 4.8, downloadCount: '500万+' }; build() { Column({ space: 20 }) { Text('首页') .fontSize(24) .fontWeight(FontWeight.Bold); Button('跳转到详情页并传递数据') .width(260) .height(40) .backgroundColor('#007DFF') .fontColor(Color.White) .onClick(() => { router.pushUrl({ url: 'pages/DetailSlice', params: this.appInfo // 传递数据 }, router.RouterMode.Single); }); } .width('100%') .height('100%') .justifyContent(FlexAlign.Center); } } // DetailSlice.ets - 详情页 import { router } from '@ohos.router'; @Entry @Component struct DetailSlice { // 接收传递的数据 @State appInfo: any = {}; onPageShow() { // 获取传递的参数 this.appInfo = router.getParams() || {}; } build() { Column({ space: 20 }) { Text('详情页') .fontSize(24) .fontWeight(FontWeight.Bold); Text(`应用名称:${this.appInfo.name}`) .fontSize(16); Text(`评分:${this.appInfo.rating}`) .fontSize(16); Text(`下载量:${this.appInfo.downloadCount}`) .fontSize(16); Button('返回首页') .width(200) .height(40) .backgroundColor('#007DFF') .fontColor(Color.White) .onClick(() => { router.back(); }); } .width('100%') .height('100%') .justifyContent(FlexAlign.Center); } }// MainSlice.ets - 首页 import { router } from '@ohos.router'; @Entry @Component struct MainSlice { @State result: string = ''; async onPageShow() { // 监听返回结果 const callback = (data: any) => { this.result = data; }; router.on('routerBack', callback); } build() { Column({ space: 20 }) { Text('首页') .fontSize(24) .fontWeight(FontWeight.Bold); Text(`详情页返回结果:${this.result}`) .fontSize(16) .fontColor('#666666'); Button('跳转到详情页') .width(200) .height(40) .backgroundColor('#007DFF') .fontColor(Color.White) .onClick(() => { router.pushUrl({ url: 'pages/DetailSlice' }, router.RouterMode.Single); }); } .width('100%') .height('100%') .justifyContent(FlexAlign.Center); } } // DetailSlice.ets - 详情页 import { router } from '@ohos.router'; @Entry @Component struct DetailSlice { private inputText: string = ''; build() { Column({ space: 20 }) { Text('详情页') .fontSize(24) .fontWeight(FontWeight.Bold); TextInput({ placeholder: '输入返回结果' }) .width(240) .height(40) .backgroundColor(Color.White) .onChange((value: string) => { this.inputText = value; }); Button('返回首页并传递结果') .width(240) .height(40) .backgroundColor('#007DFF') .fontColor(Color.White) .onClick(() => { // 传递结果并返回 router.back({ params: this.inputText }); }); } .width('100%') .height('100%') .justifyContent(FlexAlign.Center); } }应用场景:单个组件内部的状态共享
@Component struct CounterComponent { @State count: number = 0; build() { Column({ space: 10 }) { Text(`计数:${this.count}`) .fontSize(20); Button('+1') .width(100) .height(40) .onClick(() => { this.count++; }); Button('-1') .width(100) .height(40) .onClick(() => { this.count--; }); } } }应用场景:父子组件间的状态同步
// 子组件 @Component struct ChildComponent { @Prop title: string; @Link count: number; // 双向绑定 build() { Column({ space: 10 }) { Text(this.title) .fontSize(18); Button('子组件+1') .width(140) .height(40) .onClick(() => { this.count++; // 会同步到父组件 }); } } } // 父组件 @Entry @Component struct ParentComponent { @State title: string = '父子组件状态同步'; @State count: number = 0; build() { Column({ space: 20 }) { Text(`父组件计数:${this.count}`) .fontSize(20); ChildComponent({ title: this.title, count: $count // 双向绑定符号 }); Button('父组件+1') .width(140) .height(40) .onClick(() => { this.count++; // 会同步到子组件 }); } .width('100%') .height('100%') .justifyContent(FlexAlign.Center); } }应用场景:多个页面/组件间的状态共享
// globalState.ts - 全局状态文件 export const globalState = { userInfo: { name: '小明', age: 25 }, theme: 'light', language: 'zh-CN' }; // 使用全局状态的组件 import { globalState } from '../utils/globalState'; @Entry @Component struct ProfileComponent { @State userInfo: any = globalState.userInfo; build() { Column({ space: 15 }) { Text(`用户名:${this.userInfo.name}`) .fontSize(18); Text(`年龄:${this.userInfo.age}`) .fontSize(18); Button('修改用户名') .width(160) .height(40) .onClick(() => { globalState.userInfo.name = '小红'; this.userInfo = Object.assign({}, globalState.userInfo); // 触发UI更新 }); } .width('100%') .height('100%') .justifyContent(FlexAlign.Center); } }对于复杂应用,推荐使用鸿蒙官方或第三方状态管理库(如@ohos/store):
// 安装并导入状态管理库 import { Store } from '@ohos/store'; // 初始化状态 const store = new Store({ state: { count: 0 }, mutations: { increment(state: any) { state.count++; }, decrement(state: any) { state.count--; } } }); // 使用状态管理的组件 @Entry @Component struct CounterComponent { @State count: number = store.state.count; build() { Column({ space: 10 }) { Text(`计数:${this.count}`) .fontSize(20); Button('+1') .width(100) .height(40) .onClick(() => { store.commit('increment'); this.count = store.state.count; }); Button('-1') .width(100) .height(40) .onClick(() => { store.commit('decrement'); this.count = store.state.count; }); } } }为页面跳转添加过渡动画,提升用户体验:
// MainSlice.ets - 首页 import { router } from '@ohos.router'; @Entry @Component struct MainSlice { build() { Column({ space: 20 }) { Text('首页') .fontSize(24) .fontWeight(FontWeight.Bold); Button('跳转到详情页(带动画)') .width(260) .height(40) .backgroundColor('#007DFF') .fontColor(Color.White) .onClick(() => { router.pushUrl({ url: 'pages/DetailSlice' }, router.RouterMode.Single, { // 配置转场动画 animationType: router.RouterAnimationType.Slide, animationDuration: 300, animationCurve: router.RouterAnimationCurve.EaseOut }); }); } .width('100%') .height('100%') .justifyContent(FlexAlign.Center); } }在数据加载或耗时操作时,显示加载状态:
@Component struct LoadingComponent { @Prop isLoading: boolean; build() { if (this.isLoading) { Stack({ alignContent: Alignment.Center }) { LoadingProgress() .width(40) .height(40) .color('#007DFF'); Text('加载中...') .fontSize(14) .fontColor('#666666') .margin({ top: 60 }); } .width('100%') .height('100%') .backgroundColor('rgba(255, 255, 255, 0.8)'); } } } // 使用加载组件 @Entry @Component struct DataPage { @State isLoading: boolean = true; @State dataList: Array<any> = []; async onPageShow() { // 模拟网络请求 this.isLoading = true; setTimeout(() => { this.dataList = [{ id: 1, name: '数据1' }, { id: 2, name: '数据2' }]; this.isLoading = false; }, 2000); } build() { Column() { Text('数据列表') .fontSize(24) .fontWeight(FontWeight.Bold) .margin({ top: 32 }); // 渲染数据列表 List() { ForEach(this.dataList, (item) => { ListItem() { Text(item.name) .fontSize(16) .padding(16); } }, (item) => item.id); } // 加载组件 LoadingComponent({ isLoading: this.isLoading }); } .width('100%') .height('100%'); } }问题:调用跳转方法后无响应
排查方向:
config.json中注册)AbilitySlice已正确继承AbilitySlice类问题:页面间传递的数据无法获取
排查方向:
onPageShow()中获取参数,而非onStart()问题:修改状态后UI未更新
排查方向:
@State/@Prop/@Link等状态装饰器通过本章学习,我们掌握了:
通过不断实践与拓展,你将逐步掌握鸿蒙应用交互设计的精髓,构建出流畅、易用的高质量应用!