HarmonyOS鸿蒙Next企业面试题之页面跳转传递的参数

分别说Router如何传参,Navigation如何传参

router

定义模型类 pushUrl的时候params跳转传值

class ParamsModel {
  pId: number;
  cId: number;

  constructor(pId: number,cId: number) {
    this.pId=pId
    this.cId=cId
  }
}
...

 this.getUIContext().getRouter().pushUrl({
     url: 'pages/Product', // 目标url
     params: new ParamsModel(123,20)  // 添加params属性,传递自定义参数
})

this.getUIContext().getRouter().getParams()获取上个页面传值的方法

class ParamsModel {
  pId: number;
  cId: number;

  constructor(pId: number,cId: number) {
    this.pId=pId
    this.cId=cId
  }
}

@State pId:number =(this.getUIContext().getRouter().getParams() as ParamsModel).pId
@State cId:number=(this.getUIContext().getRouter().getParams() as ParamsModel).cId

Navigation

1、定义模型类
export class ParamsModel {
  pid: string | number;
  cid: string| number;
  constructor(pid: string| number, cid: string| number) {
    this.pid = pid
    this.cid = cid
  }
}
2、param跳转传值
this.pathStack.pushPath({
            name:"LoginPage",
            param: new ParamsModel(12,34)
})
3、context.pathInfo.param获取传值
pathStack: NavPathStack = new NavPathStack();

...


NavDestination() {
      ...
}   
 .hideTitleBar(true)
 .onReady((context: NavDestinationContext) => {
      this.pathStack = context.pathStack
      let loginParam:ParamsModel = context.pathInfo.param as ParamsModel;
      console.log("loginParam:"+JSON.stringify(loginParam))
 })

更多关于HarmonyOS鸿蒙Next企业面试题之页面跳转传递的参数的实战教程也可以访问 https://www.itying.com/category-93-b0.html

回到顶部