Harmony NavPathStack 页面跳转参数传递

149 阅读1分钟

1 子页面中NavPathStack获取

上一篇文章中说在非首页页面中通过onReady中获取NavPathStack

image.png

另外一种方式是 主页声明

@Provide('pageStack')
pageStack: NavPathStack = new NavPathStack()

其他页面引用

@Consume('pageStack')
pageStack: NavPathStack

2 参数获取

网上很多写法

第一种 写法

let arg =  (this.pageStack.getParamByName("Goods") ?? []) as [];

Goods 为from页面的名称。但是这个有问题。如果是多个不同的页面跳转相同的页面,这里怎么处理。

第二种

let arg =  (this.pageStack.getParamByIndex(0) ?? new Map()) as Map<String,object>;

一样的问题,没办法确定from页面的下标index。

第三种 在onReady中获取 from页面传递的 NavPathInfo

.onReady((context)=>{
  let pathInfo = context.pathInfo;
  console.log("====pathInfo===="+json.stringify(pathInfo));
})