中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
微信扫码分享
import router from '@ohos.router' import { BusinessError } from '@ohos.base' @Entry @Component struct Page6181 { @State str: string = '' onPageShow(): void { const params = (router.getParams() as Record<string, string>); if (params) { const info: string = params.info as string; // 获取info属性的值 this.str = info } } async routePage() { let options:router.RouterOptions = { url: 'pages/Page6182', params: { info:"第一页的值" } } try { await router.pushUrl(options) } catch (err) { console.info(` fail callback, code: ${(err as BusinessError).code}, msg: ${(err as BusinessError).message}`) } } build() { Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { Text('这是第一页') .fontSize(50) .fontWeight(FontWeight.Bold) Button() { Text('next page') .fontSize(25) .fontWeight(FontWeight.Bold) }.type(ButtonType.Capsule) .margin({ top: 20 }) .backgroundColor('#ccc') .onClick(() => { this.routePage() }) Row(){ Text(`这是第二页传过来的值:${this.str}`) .fontSize(50) .fontWeight(FontWeight.Bold) } } .width('100%') .height('100%') } } Page6182 import router from '@ohos.router' import { BusinessError } from '@kit.BasicServicesKit' @Entry @Component struct Page6182 { async routePage() { let options:router.RouterOptions = { url: 'pages/Page6181', params: { info:"第二页的值" } } try { await router.pushUrl(options) } catch (err) { console.info(` fail callback, code: ${(err as BusinessError).code}, msg: ${(err as BusinessError).message}`) } } onPageShow(): void { this.secondData = this.text } private content: string = "这是第二页" @State text: string = (router.getParams() as Record<string, string>).info @State secondData: string = '' build() { Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { Text(`${this.content}`) .fontSize(50) .fontWeight(FontWeight.Bold) Text(`第一页传来的数值:${this.secondData}`) .fontSize(20) .margin({ top: 20 }) .backgroundColor('red') Button("携带参数返回") .onClick(() => { this.routePage() }) } .width('100%') .height('100%') } }