HarmonyOS 鸿蒙Next如何切换navigation

HarmonyOS 鸿蒙Next如何切换navigation 有没有router和navigation的混用方案

3 回复

您可以查看此文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-router-to-navigation-V5#架构差异

router和navigation的架构不同,router.pushUrl跳转的是@entry修饰的页面,NavPathStack也无法控制@entry的页面。您可以使用router.page1携带参数返回,Navigation根据参数加载NavDestination2。

您项目的封装的页面是以什么方式加载的?

router和navigation的生命周期和路由控制是相互独立的,需要分开处理,您可参考以下思路:

将封装的页面以公共组件的方式加载到NavDestination里(Navigation->NavDestination->NavDestination1(router.page1)->NavDestination2)

需要在router跳转到的页面使用Navigation组件重新开启新的Navigation。可参考以下demo:

//index.ets (在main_page.json中注册)
@Entry
@Component
struct Index {
  pageInfo: NavPathStack = new NavPathStack()
  build() {
    Navigation(this.pageInfo) {
      Column() {
        Button('StartTest', { stateEffect: true, type: ButtonType.Capsule })
          .width('80%')
          .height(40)
          .margin(20)
          .onClick(() => {
            this.pageInfo.pushPath({ name: 'pageOne' })
          })
      }
    }.title('NavIndex')
  }
}

//PageOne.ets (需要注册routermap)
import { router } from '@kit.ArkUI';
@Builder
export function PageOneBuilder(name: string, param: Object) {
  PageOne()
}
@Component
struct PageOne {
  pageInfo: NavPathStack = new NavPathStack();
  @State message: string = 'pageOne'
  aboutToAppear(): void {
  }
  build() {
    NavDestination() {
      Text(this.message)
        .width('80%')
        .height(50)
        .margin(10)
      Button('pushPath', { stateEffect: true, type: ButtonType.Capsule })
        .width('80%')
        .height(40)
        .margin(10)
        .onClick(() => {
          //使用router跳转到下一个页面
          router.pushUrl({
            url: "pages/PageRouterOne"
          })
        })
    }
    .height('100%')
    .width('100%')
  }
}

//PageRouterOne.ets (在main_page.json中注册)
@Entry
@Component
struct PageRouterOne {
  pageInfo: NavPathStack = new NavPathStack();
  @State message: string = 'Hello World';
  build() {
    Navigation(this.pageInfo) {
      Text(this.message)
        .id('PageRouterOneHelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: 'container', align: VerticalAlign.Center },
          middle: { anchor: 'container', align: HorizontalAlign.Center }
        })
      Button("看看下面").onClick(() => {
        this.pageInfo.pushPath({ name: "pageTwo" })
      })
    }
    .height('100%')
    .width('100%')
  }
}

//PageTwo.ets(需要注册routermap)
@Builder
export function PageTwoBuilder(name: string, param: Object) {
  PageOne()
}
@Component
struct PageOne {
  pageInfo: NavPathStack = new NavPathStack();
  @State message: string = 'pageTwo'
  build() {
    NavDestination() {
      Text(this.message)
        .width('80%')
        .height(50)
        .margin(10)
      Button('pushPath', { stateEffect: true, type: ButtonType.Capsule })
        .width('80%')
        .height(40)
        .margin(10)
    }
    .height('100%')
    .width('100%')
  }
}

//router_map.json
{
  "routerMap": [
    {
      "name": "pageOne",
      "pageSourceFile": "src/main/ets/pages/PageOne.ets",
      "buildFunction": "PageOneBuilder",
      "data": {
        "description": "this is pageOne"
      }
    },
    {
      "name": "pageTwo",
      "pageSourceFile": "src/main/ets/pages/PageTwo.ets",
      "buildFunction": "PageTwoBuilder"
    }
  ]
}

更多关于HarmonyOS 鸿蒙Next如何切换navigation的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


姓名: 张三
职业: 软件工程师
简介: 拥有超过10年的IT行业经验,擅长Java和Python开发。

在HarmonyOS(鸿蒙Next)中,切换navigation通常涉及使用Navigator组件和Router模块。以下是一个简单的示例,展示如何在两个页面之间进行切换:

  1. 定义路由:首先,在config.json文件中定义页面路由。
{
  "pages": [
    "pages/index/index",
    "pages/detail/detail"
  ]
}
  1. 创建页面:在pages目录下创建两个页面文件,例如index.etsdetail.ets
// index.ets
import router from '@ohos.router';

@Entry
@Component
struct Index {
  build() {
    Column() {
      Button('Go to Detail')
        .onClick(() => {
          router.push({ url: 'pages/detail/detail' });
        });
    }
  }
}
// detail.ets
import router from '@ohos.router';

@Entry
@Component
struct Detail {
  build() {
    Column() {
      Button('Back to Index')
        .onClick(() => {
          router.back();
        });
    }
  }
}
  1. 切换页面:在index.ets中,使用router.push方法跳转到detail.ets页面;在detail.ets中,使用router.back方法返回到index.ets页面。

通过以上步骤,你可以在HarmonyOS中实现基本的页面导航切换。

回到顶部