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' ) ) }
}