HarmonyOS router.back\(1\)失效,无法正常返回上一个页面?

使用router.pushNamedRoute()跳转登录页面(har包),登录成功后,调用router.back(1)无法正常返回原来的页面。

另外请问,在封装的请求类A中,如果要实现NavPathStack方式跳转登录页面(就是把登录页面改造成Navigation方式),请问在A中,如何事项appPathStack.pushPath跳转(因为在类A中好像无法获取到appPathStack。现在是在首页通过@Provide 来把appPathStack注册到所有子组件上的,然后整个项目是router+Navigation混合使用的)----尝试通过这种方案解决上面这个问题

阅读 479
1 个回答

您的路由跳转时hsp跳转到hap包吗,如果是这种模式的话,这边尝试了跳转之后使用router.back(1)可以返回页面,这边给您提供了下面的参考:

方案一:使用页面命名路由实现共享hap包、hsp包页面间的跳转返回。使用该方法时要注意:

1、在当前应用包的oh-package.json5文件中配置共享包依赖;

2、为即将要跳转的共享包页面进行自定义命名;

3、引入共享包中的命名路由页面。

方案二:使用普通的页面路由跳转,但要详细指定url的内容,内容模板为:’@bundle:包名(bundleName)/模块名(moduleName)/路径/页面所在的文件名(不加.ets后缀)’,其中“bundle”的值为app.json5文件中的“bundleName”。

方案一参考链接:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-routing-V5\#命名路由

方案二参考链接:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/in-app-hsp-V5

demo如下:

//当前应用包参考代码
import { router } from '@kit.ArkUI';

const module = import('sharedlibrary/src/main/ets/pages/Index')

@Entry({ routeName: 'myPage' })
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)

        //方案一:页面命名路由
        Button('点击跳转hsp')
          .onClick(()=>{
            router.pushNamedRoute({
              name:'hspPage',
            })
          })

        //方案二:页面模块间跳转
        // Button('点击跳转')
        //   .onClick(()=>{
        //     router.pushUrl({
        //       url:  '@bundle:com.example.personnaltrial/sharedlibrary/ets/pages/Index'
        //     })
        //   })
      }
      .width('100%')
    }
    .height('100%')
  }
}
//hsp包(包具体名称为sharedlibrary)页面代码:
import { router } from '@kit.ArkUI';


@Entry({ routeName: 'hspPage' })
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    Row() {
      Column() {


        //对应方案一
        Button('点击返回hap')
          .onClick(()=>{
            router.pushNamedRoute({
              name:'myPage',
            })
          })
          //如果当前处于HSP中的页面,需要返回之前的页面时,可以使用router.back方法,但是返回的页面必须是当前页面跳转路径上的页面。
          .onClick(()=>{
            router.back()
          })

        //对应方案二
        // Button('点击返回hap')
        //   .onClick(()=>{
        //     router.replaceUrl({
        //       url:'@bundle:com.example.personnaltrial/hap1/ets/pages/Index'
        //     })
        // /*如果当前处于HSP中的页面,需要返回之前的页面时,可以使用router.back方法,但是返回的页面必须是当前页面跳转路径上的页面。*/
        //     // router.back()
        //   })
      }
      .width('100%')
    }
    .height('100%')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进