IndexPage
- 定义一个参数类
export class RouterParams {
src:string
constructor(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'))
}
}
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » HarmonyOS页面跳转和页面间传值
发表评论 取消回复