HarmonyOS鸿蒙Next中如何用NavPathStack跳转到shared model并返回信息

HarmonyOS鸿蒙Next中如何用NavPathStack跳转到shared model并返回信息 通过

this.customPathInfos.pushPathByName("PickerIndexBuilder","")跳转到shared model的page 但跳转不成功  在shared model的route_map中添加了 {
  "routerMap": [
    {
      "name": "IndexBuilder",
      "pageSourceFile": "src/main/ets/pages/Index.ets",
      "buildFunction": "PickerIndexBuilder"
    }
  ]
}
module.json5中加了
"routerMap": "$profile:route_map"  请有大神知道如何改进吗?

更多关于HarmonyOS鸿蒙Next中如何用NavPathStack跳转到shared model并返回信息的实战教程也可以访问 https://www.itying.com/category-93-b0.html

10 回复

入口模块entry的 oh-package.json5 同样需要添加 hsp包的依赖。

"dependencies": {
  "@cxy/xxx": "file:../common/xxx",
}

更多关于HarmonyOS鸿蒙Next中如何用NavPathStack跳转到shared model并返回信息的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


//根据资源查询对应的信息
this.customPathInfos.pushDestinationByName("PickerIndexBuilder","")
  .then((v)=>{
    console.info('跳转成功');
  })
  .catch((v:BusinessError)=>{
    console.error("abc",JSONUtil.beanToJsonStr(v))
  })

"code":100005,"message":"Builder function not registered"

缺少builder函数,需要在filepicker模块的 Index.ets 中加上:

@Builder
function IndexBuilder()  {
    Index()
}
import { ApiUtilPicker } from './ApiUtilPicker';

@Entry
@Component
export struct Index {
  @State message: string = 'Hello World';

  // 在Navigation中注册页面(需在入口组件中配置)

  aboutToAppear(): void {
    console.log("abc","aborttoappera")
  }
  build() {

    NavDestination(){

      Column() {
        Text("Column").width("100%").height("100%")

      }.height("100%")
      .width("100%")
      .justifyContent(FlexAlign.SpaceBetween)

    }
    .hideTitleBar(true)
    .width("100%").height("100%")
  }
}

//PageOne.ets
@Builder
export function PickerIndexBuilder(name: string, param: string) {
  Index();
}

pushPathByName参数传入name字段,应该是–IndexBuilder

感谢帮助 不过这个page指定的名称为"buildFunction": “PickerIndexBuilder” 这个 IndexBuilder这个没有注册,

看看这个Demo:对比一下你的代码少写了啥:

  @Entry
  @Component
  struct Index {
    pageStack : NavPathStack = new NavPathStack();
    build() {
      Navigation(this.pageStack){
      }.onAppear(() => {
        this.pageStack.pushPathByName("PageOne", null, false);
      })
      .hideNavBar(true)
    }
  }
  // 跳转页面入口函数
  @Builder
  export function PageOneBuilder() {
    PageOne();
  }
  @Component
  struct PageOne {
    pathStack: NavPathStack = new NavPathStack();
    build() {
      NavDestination() {
      }
      .title('PageOne')
      .onReady((context: NavDestinationContext) => {
         this.pathStack = context.pathStack;
      })
    }
  }

感谢帮助 没有缺 在同模块中 跳转没问题 但跨模块跳转会不成功,

在HarmonyOS Next中,使用NavPathStack跳转到shared model并返回信息,可通过NavDestination的onResult回调实现。在目标页面设置返回数据:router.back({ data: { key: 'value' } })。源页面在NavDestination的onResult中接收:onResult: (result) => { if (result) { // 处理result.data } }。确保路径配置正确。

在HarmonyOS Next中,使用NavPathStack跳转到Shared Model的页面并返回信息,需要确保跨模块路由配置正确。根据你的代码,问题可能出在以下几个方面:

  1. 路由名称不匹配:你代码中push的路径名是"PickerIndexBuilder",但route_map中配置的name"IndexBuilder"。这两个名称必须完全一致才能跳转成功。请将pushPathByName的第一个参数改为"IndexBuilder"

  2. 模块依赖声明:在调用方的module.json5中,需要声明对Shared Model的依赖。确保在"dependencies"数组中包含了目标模块。

  3. 返回信息传递:要从Shared Model页面返回信息,建议使用页面路由的callback方式。示例:

// 跳转时传递callback
this.customPathInfos.pushPathByName("IndexBuilder", "", (result: Object) => {
  // 处理返回的结果
  console.log("返回的数据:", result);
});

// 在Shared Model的Index.ets页面中,返回时调用
router.back({
  url: 'pages/Index',
  params: { data: '返回的信息' }
});
  1. 完整配置检查:确保Shared Model的route_map.json文件路径正确,且在模块的module.json5中正确引用了该profile文件。

请先修正路由名称一致性问题,这是导致跳转失败的最常见原因。如果问题仍然存在,请检查控制台是否有相关错误日志。

回到顶部