您的位置:首页 > 游戏 > 手游 > HarmonyOS页面跳转和页面间传值

HarmonyOS页面跳转和页面间传值

2024/10/6 2:25:03 来源:https://blog.csdn.net/yu540135101/article/details/140554631  浏览:    关键词:HarmonyOS页面跳转和页面间传值

IndexPage

在这里插入图片描述

  • 定义一个参数类
export class RouterParams {src:stringconstructor(str:string) {this.src = str}
}
  • 点击按钮后,将创建的RouterParams 对象传递到第二页
        Button($r('app.string.next')).fontSize(16).width(300).height(50).backgroundColor($r('app.color.button_bg')).onClick(() => {router.pushUrl({url: 'pages/SecondPage',params: new RouterParams('Index页面传来的数据')}).catch((error:Error) => {Logger.info(TAG, 'IndexPage push error' + JSON.stringify(error));});})

SecondPage

在这里插入图片描述


import { router } from '@kit.ArkUI';
import CommonConstants from '../common/constants/CommonConstants';
import { RouterParams } from './IndexPage';@Entry
@Component
struct SecondPage {@State params: RouterParams = router.getParams() as RouterParams@State src: string = this.params.src;build() {Row() {Column() {Text('第二页').fontSize(38).fontWeight(FontWeight.Bold)Text(this.src).fontSize(16)Blank()Button($r('app.string.back')).fontSize(16).width(300).height(50).backgroundColor($r('app.color.button_bg')).onClick(() => {router.back();})}.width(CommonConstants.FULL_WIDTH).height(CommonConstants.LAYOUT_HEIGHT)}.height(CommonConstants.FULL_HEIGHT).backgroundColor($r('app.color.page_bg'))}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com