HarmonyOS鸿蒙Next应用开发中,Navigation右边保持只有一个页面

HarmonyOS鸿蒙Next应用开发中,Navigation右边保持只有一个页面

Navigation没有原生接口实现这个效果,只能根据NavPathStack提供的接口来手动维护,例如先通过size接口判断栈中页面的个数,为0就添加,不为0就替换,示例如下:

Button('打开 页面2').width('80%').margin({ top: 10, bottom: 10 })
  .onClick(() => {
    if (this.pathStack.size() == 0) {
      this.pathStack.pushPath({ name: 'Page02' });
    } else {
      this.pathStack.replacePath({ name: 'Page02' });
    }
  })

效果如下:

效果

完整示例如下:

@Component
struct Page01 {
  pathStack: NavPathStack | undefined = undefined;
  @State pageId: string = '';

  build() {
    NavDestination() {
      Text(`页面ID: ${this.pageId}`).width('100%').padding(10).fontSize(22)
      Button('打开 页面2').width('80%').margin({ top: 10, bottom: 10 })
        .onClick(() => {
          if (this.pathStack?.size() == 0) {
            this.pathStack?.pushPath({ name: 'Page02' });
          } else {
            this.pathStack?.replacePath({ name: 'Page02' });
          }
        })
      Button('返回').width('80%').margin({ top: 10, bottom: 10 })
        .onClick(() => {
          this.pathStack?.pop();
        })
    }.title('页面1')
    .onReady((context: NavDestinationContext) => {
      this.pathStack = context.pathStack;
      this.pageId = context.navDestinationId as string;
    })
  }
}

@Component
struct Page02 {
  pathStack: NavPathStack | undefined = undefined;
  @State pageId: string = '';

  build() {
    NavDestination() {
      Text(`页面ID: ${this.pageId}`).width('100%').padding(10).fontSize(22)
      Button('打开 页面1').width('80%').margin({ top: 10, bottom: 10 })
        .onClick(() => {
          if (this.pathStack?.size() == 0) {
            this.pathStack?.pushPath({ name: 'Page01' });
          } else {
            this.pathStack?.replacePath({ name: 'Page01' });
          }
        })
      Button('返回').width('80%').margin({ top: 10, bottom: 10 })
        .onClick(() => {
          this.pathStack?.pop();
        })
    }.title('页面2')
    .onReady((context: NavDestinationContext) => {
      this.pathStack = context.pathStack;
      this.pageId = context.navDestinationId as string;
    })
  }
}

@Entry
@Component
struct Index {
  pathStack: NavPathStack = new NavPathStack();

  @Builder
  pagesMap(name: string) {
    if (name == 'Page01') {
      Page01()
    } else if (name == 'Page02') {
      Page02()
    }
  }

  build() {
    Navigation(this.pathStack) {
      Button('打开 页面1').width('80%').margin({ top: 10, bottom: 10 })
        .onClick(() => {
          if (this.pathStack.size() == 0) {
            this.pathStack.pushPath({ name: 'Page01' });
          } else {
            this.pathStack.replacePath({ name: 'Page01' });
          }
        })
      Button('打开 页面2').width('80%').margin({ top: 10, bottom: 10 })
        .onClick(() => {
          if (this.pathStack.size() == 0) {
            this.pathStack.pushPath({ name: 'Page02' });
          } else {
            this.pathStack.replacePath({ name: 'Page02' });
          }
        })
      Button('返回').width('80%').margin({ top: 10, bottom: 10 })
        .onClick(() => {
          this.pathStack.pop();
        })
    }.title('主页面')
    .titleMode(NavigationTitleMode.Mini)
    .navDestination(this.pagesMap)
  }
}

更多关于HarmonyOS鸿蒙Next应用开发中,Navigation右边保持只有一个页面的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

在HarmonyOS鸿蒙Next应用开发中,Navigation组件默认支持多页面导航。若需右边保持只有一个页面,可通过设置NavigationsinglePage属性为true来实现。此配置确保导航栈中仅保留一个页面,新页面会替换当前页面,而非压入栈中。适用于单页面应用或需限制页面堆栈的场景。

更多关于HarmonyOS鸿蒙Next应用开发中,Navigation右边保持只有一个页面的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS Next中实现Navigation右侧只保留一个页面的需求,确实需要手动维护NavPathStack。您提供的代码方案是正确的实现方式,通过判断栈大小来决定使用pushPath还是replacePath。

关键点分析:

  1. 使用pathStack.size()判断当前栈中页面数量
  2. 当size为0时使用pushPath添加新页面
  3. 当size不为0时使用replacePath替换当前页面

这种实现方式相比默认的多页面堆栈有以下优势:

  • 避免页面无限叠加导致内存问题
  • 保持简洁的页面层级关系
  • 更符合某些特定场景的导航需求

您的示例代码已经完整展示了这种实现方式,包括页面间的双向导航和返回逻辑。需要注意的是,这种模式下返回操作会直接回到根页面,而不是前一个页面,这是符合预期的行为。

回到顶部