在pages目录下创建第二个页面Index1.ets
创建文件后main_pages.json中会自动添加文件路径
演示代码参考如下,即可实现跳转第二个页面以及返回。
第一个页面
import router from '@ohos.router';
@Entry
@Component
struct Index {
@State message: string = '第一个页面';
build() {
Row() {
Column({ space: 20 }) {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
Button("下一个页面").onClick(() => {
console.log("点击下一页按钮");
router.pushUrl({ url: "pages/Index1" })
})
}
.width('100%')
}
.height('100%')
}
}
第二个页面
import router from '@ohos.router';
@Entry
@Component
struct Index1 {
@State message: string = '第二个页面';
build() {
Row(){
Column({space:20}) {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
Button("返回").onClick(()=>{
router.back()
})
}
.width('100%')
}
.height('100%')
}
}