> 文档中心 > HarmonyOS开发DecEco Studio页面跳转

HarmonyOS开发DecEco Studio页面跳转

一、pages创建新页面
在index.ets增加路由:
import router from ‘@ohos.router’; //页面路由跳转

import router from '@ohos.router';let msg:string='Index 页面传来的数据';//数据@Entry@Componentstruct Index {  @State message: string = 'Hello World'  build() {    Row() {      Column() { Text(this.message)   .fontSize(50)   .fontWeight(FontWeight.Bold) Button('Exit')   .width('80%').height(40)   .onClick(()=>{     router.push({url:'pages/Second',params:{//在router.push方法时传递src数据  src:msg,}     });   })      }      .width('100%').height(140)    }    .height('100%')  }}

router.push()执行跳转
url:‘pages/Second’, //页面途径
params:{
src:msg, //传递数据

import router from '@ohos.router';@Entry@Componentstruct Second {  @State message: string = 'Hello World'  @State src:string = router.getParams()?.['src'];//获取传递的数据  build() {    Row() {      Column() { Text(this.message)//创建字体   .fontSize(50)   .fontWeight(FontWeight.Bold) Text(this.src)//创建字体  展示传递的数据   .fontSize(20)   .fontColor(Color.Red)//字体颜色      }      .width('100%')    }    .height('100%')  }}```