[Harmony]Router跳转新页面和关闭回传数据

使用router.pushUrl跳转新页面,在onPageShow方法接收后面页面的回调。后面页面中,使用router.back关闭页面和回传数据。

注意onPageShow方法不执行原因:

若当前组件未通过router.pushUrl导航或未在pages.json中配置为路由页面,onPageShow不会触发。

自定义组件默认无页面生命周期,需显式声明@Entry装饰器。

MFRechargeAddMemberView

TypeScript 复制代码
export interface MFRechargeAddMemberParams {
  inputSubmit?: boolean // 输入提交
  inputPassword?: string // 输入的密码
}

@Entry
@Component
struct MFRechargeAddMemberView {
  @State saveZFMMText: string = '' // 支付密码 

  onPageShow() {
    // 后面页面的回调
    const params = this.getUIContext().getRouter().getParams() as MFRechargeAddMemberParams;
    if (params?.inputSubmit == true) {
      let pw = params.inputPassword
      if (pw != undefined && !isBlank(pw)) {
        this.saveZFMMText = pw
      } else {
        this.saveZFMMText = ''
      }
    }
  }

  build() {
    Column() {
        Button('支付密码设置') 
        .onClick(() => {
           this.tapPayPassword()
        })
    }
    .width('100%')
    .height('100%')
  }

  // 支付密码设置
  private tapPayPassword() {
    this.getUIContext().getRouter().pushUrl({
      url: 'pages/features/memberRecharge/MFPayPasswordSetView',
      params: {
        parIsEdit: false,
      }
    }, router.RouterMode.Standard, (err) => {
      if (!err) {
        //console.log('pushUrl成功')
      }
    })
  }
  
}

MFPayPasswordSetView

TypeScript 复制代码
export interface MFPayPasswordSetParams {
  parIsEdit: boolean,
}

@Entry
@Component
struct MFPayPasswordSetView {
  @State isEdit: boolean = false
  
  @State private usePassword: string = ''

  aboutToAppear() {
    const params = this.getUIContext().getRouter().getParams() as MFPayPasswordSetParams;
    if (params) {
      this.isEdit = params.parIsEdit
    }
  }

  build() {
    Column() {
       Button('返回') 
        .onClick(() => {
            //this.getUIContext().getRouter().back();
            this.getUIContext().getRouter().back({
              url: 'pages/features/memberRecharge/MFRechargeAddMemberView',
              params: {
                inputSubmit: true,
                inputPassword: this.usePassword
              }
            })
        })
    }
    .justifyContent(FlexAlign.Start)
    .width('100%')
    .height('100%')
    .onAppear(()=>{

    })
  }   

main_pages.json

TypeScript 复制代码
{
  "src": [
    "pages/Index",

    "pages/features/memberRecharge/MFRechargeAddMemberView",
    "pages/features/memberRecharge/MFPayPasswordSetView",
  ]
}
相关推荐
Irene19912 天前
Vue 3 中使用 Vue Router 实现 SPA(单页应用)
vue.js·spa·router
aqi006 天前
新书《鸿蒙HarmonyOS 6应用开发:从零基础到App上线》出版啦
harmonyos·鸿蒙·harmony
一只小阿乐14 天前
vue 改变查询参数的值
前端·javascript·vue.js·路由·router·网文·未花中文网
malajisi012 个月前
鸿蒙PC开发笔记一:HarmonyOS PC 命令行适配指南(Mac 版)
笔记·macos·harmonyos·harmony·鸿蒙pc·harmony pc
RollingPin3 个月前
iOS八股文之 组件化
ios·路由·router·组件化·imp·分层设计
hashiqimiya3 个月前
鸿蒙harmony将注册的数据包装成json发送到后端的细节及过程
harmony
逐云者1233 个月前
使用 FastAPI 构建大模型应用的系统教程(工程化实战指南)
大模型·fastapi·router·分层架构·算法工程·算法服务
爱听歌的周童鞋3 个月前
斯坦福大学 | CS336 | 从零开始构建语言模型 | Spring 2025 | 笔记 | Lecture 4: Mixtrue of experts
llm·router·moe·cs336·deepseek-moe
花菜会噎住3 个月前
Vue3 路由配置和使用与讲解(超级详细)
开发语言·javascript·ecmascript·路由·router