鸿蒙_组件导航Navigation的基础用法

ArkUI组件导航,可以在一个页面内虚拟出多个组件导航页面,我们通过案例来测试一下基础用法:

TypeScript 复制代码
@Entry
@Component
struct TestNavigation {
  @State message: string = 'Test Navigation';
  //定义一个页面栈(用于管理页面,记录页面路径信息及访问顺序等)
  navPathStack: NavPathStack = new NavPathStack()

  //自定义构建函数(用来承载多个页面内容)
  @Builder
  builderPages(name: string) {
    if (name === 'pageOne') {
      //页面1内容要放到导航目标内
      NavDestination() {
        //可以直接罗列组件,不用再通过根节点包裹
        Text('页面1').fontColor(Color.Red)
        Text('的内容')
      }
    } else {
      //页面2内容也要放到导航目标内
      NavDestination() {
        //这个我们测试一下横向排布
        Row() {
          Text('页面2').fontColor(Color.Orange)
          Text('的内容')
        }
      }
    }
  }

  build() {
    Column() {
      Text(this.message)
        .fontSize(33)
        .fontWeight(FontWeight.Bold)
        .onClick(() => {
          this.message = 'Welcome';
        })

      //组件导航
      Navigation(this.navPathStack) {
        //这里写根页面
        Button('打开页面1', { stateEffect: true, type: ButtonType.Capsule })
          .width('80%')
          .margin(20)
          .onClick(() => {
            this.navPathStack.pushPath({ name: 'pageOne' });
          })
        Button('打开页面2', { stateEffect: true, type: ButtonType.Capsule })
          .width('80%')
          .onClick(() => {
            this.navPathStack.pushPath({ name: 'pageTwo' });
          })
      }
      .navDestination(this.builderPages)
      .border({ width: 1, color: Color.Black })
      .margin(50)
      .backgroundColor(Color.Gray)
    }
    .height('100%')
    .width('100%')
  }
}

运行效果如下:

相关推荐
Hello__777711 小时前
开源鸿蒙 Flutter 实战|文章分类标签功能全流程实现
flutter·开源·harmonyos
GitCode官方12 小时前
一声唤醒 万物响应|AtomGit 首款开源鸿蒙 AI 硬件「小鸿」发布会圆满落幕 定义智能交互新入口
人工智能·开源·harmonyos
国医中兴13 小时前
Flutter 三方库 nhost_graphql_adapter 的鸿蒙化适配指南 - 云端数据实时对齐、GraphQL 架构实战、鸿蒙级全栈交互专家
flutter·harmonyos·graphql
nashane13 小时前
HarmonyOS 6学习:RCP远场通信流式返回实战——告别“一次性”数据阻塞
学习·华为·harmonyos
李游Leo13 小时前
别把耗时任务都丢进 async:HarmonyOS 里 TaskPool 和 Worker 的边界感
harmonyos
不喝水就会渴13 小时前
HarmonyOS 6.1 新特性:悬浮页签和沉浸光感技术实践
华为·harmonyos
心走14 小时前
鸿蒙OpenGL ES渲染H264花屏问题
harmonyos
IntMainJhy15 小时前
Flutter 三方库 get_it + flutter_bloc 的鸿蒙化适配与实战指南
flutter·华为·harmonyos
maaath15 小时前
【maaath】Flutter for OpenHarmony 定位服务能力集成指南
flutter·华为·harmonyos
maaath16 小时前
【maaath】Flutter for OpenHarmony分类筛选与标签匹配深度剖析
flutter·华为·harmonyos