当前位置: 首页 > news >正文

鸿蒙:实现两个Page页面跳转

效果展示

这篇博文在《鸿蒙:从0到“Hello Harmony”》基础上实现两个Page页面跳转

1.构建第一个页面

第一个页面就是“Hello Harmony”,把文件名和显示内容都改一下,改成“FirstPage”,再添加一个“Next”按钮。

@Entry
@Component
struct FristPage {@State message1: string = "FirstPage"@State message2: string = 'Next'build() {Row() {Column() {Text(this.message1).fontSize(30).fontWeight(FontWeight.Bold).height("10%").margin({top: 0})Button(this.message2).fontSize(30).fontWeight(FontWeight.Bold).height('5%').type(ButtonType.Capsule).margin({top: 30}).backgroundColor("#0D9FFB").width('50%').height('5%')}.width('100%')}.height('100%')}
}

在编辑窗口右上角的侧边工具栏,点击Previewer,打开预览器。第一个页面效果如下图所示:

2.构建第二个页面

1.在“entry > src > main > ets > pages”目录下,新建SecondPage.ets

同样,实现一个文本和一个Button按钮

@Entry
@Component
struct SecondPage {@State message1: string = 'SecondPage'@State message2: string = 'Back'build() {Row() {Column() {Text(this.message1).fontSize(30).fontWeight(FontWeight.Bold)Button(this.message2).fontSize(30).fontWeight(FontWeight.Bold).height('5%').type(ButtonType.Capsule).margin({top: 30}).backgroundColor("#0D9FFB").width('50%').height('5%')}.width('100%')}.height('100%')}
}

Previewer效果:

3.配置路由

配置第二个页面的路由。

在“Project”窗口,打开“entry > src > main > resources > base > profile”,

main_pages.json文件中的“src”下配置第二个页面的路由“pages/second”

代码如下:

{"src": ["pages/FirstPage","pages/SecondPage"]
}

4.实现页面跳转

​页面间的导航可以通过页面路由router来实现。

页面路由router根据页面url找到目标页面,从而实现跳转。

使用页面路由需要导入router模块。

(1).第一个页面跳转到第二个页面

在第一个页面中,跳转按钮绑定onClick事件,点击按钮时跳转到第二页。

FirstPage.ets”文件的代码如下:

// @ohos.router模块功能从API version 8开始支持,请使用对应匹配的SDK
import router from '@ohos.router';@Entry
@Component
struct FristPage {@State message1: string = "FirstPage"@State message2: string = 'Next'build() {Row() {Column() {Text(this.message1).fontSize(30).fontWeight(FontWeight.Bold).height("10%").margin({top: 0})Button(this.message2).fontSize(30).fontWeight(FontWeight.Bold).height('5%').type(ButtonType.Capsule).margin({top: 30}).backgroundColor("#0D9FFB").width('50%').height('5%').onClick(() => {console.info(`Succeeded in clicking the 'Next' button.`)try {router.pushUrl({ url: 'pages/SecondPage' })console.info('Succeeded in jumping to the second page.')} catch (err) {console.error(`Failed to jump to the second page.Code is ${err.code}, message is ${err.message}`)}})}.width('100%')}.height('100%')}
}

(1).第二个页面跳转到第一个页面

在第二个页面中,返回按钮绑定onClick事件,点击按钮时返回到第一页。

“SecondPage.ets”文件的示例如下:

import router from '@ohos.router';@Entry
@Component
struct SecondPage {@State message1: string = 'SecondPage'@State message2: string = 'Back'build() {Row() {Column() {Text(this.message1).fontSize(30).fontWeight(FontWeight.Bold)Button(this.message2).fontSize(30).fontWeight(FontWeight.Bold).height('5%').type(ButtonType.Capsule).margin({top: 30}).backgroundColor("#0D9FFB").width('50%').height('5%').onClick(() => {console.info(`Succeeded in clicking the 'Next' button.`)try {router.pushUrl({ url: 'pages/FirstPage' })console.info('Succeeded in jumping to the second page.')} catch (err) {console.error(`Failed to jump to the second page.Code is ${err.code}, message is ${err.message}`)}})}.width('100%')}.height('100%')}
}

5.实现效果

如开头展示

http://www.lryc.cn/news/236354.html

相关文章:

  • C#有关里氏替换原则的经典问题答疑
  • 【每日一题】689. 三个无重叠子数组的最大和-2023.11.19
  • “开源 vs. 闭源:大模型的未来发展趋势预测“——探讨大模型未来的发展方向
  • 计算机网络——物理层-信道的极限容量(奈奎斯特公式、香农公式)
  • 【算法挨揍日记】day31——673. 最长递增子序列的个数、646. 最长数对链
  • Jmeter做接口测试
  • 第14届蓝桥杯青少组python试题解析:23年5月省赛
  • SpringCloud 微服务全栈体系(十四)
  • 【开题报告】基于微信小程序的个人健康管理系统的设计与实现
  • Swagger笔记
  • 数据结构 堆
  • 将 ONLYOFFICE 文档编辑器与 Node.js 应用集成
  • CentOS 7搭建Gitlab流程
  • Idea安装完成配置
  • 超详细~25考研规划~感恩现在努力的你!!!
  • 智慧城市安全监控的新利器
  • 【算法】石子合并(区间dp)
  • C++-特殊类和单例模式
  • 【开源】基于Vue.js的智能教学资源库系统
  • C语言之qsort()函数的模拟实现
  • 数字化未来:实时云渲染在智慧城市中的创新应用
  • Go语言常用命令详解(二)
  • ChatGPT 从零到一打造私人智能英语学习助手
  • 算法升级之路(七)-盛最多水的容器
  • milvus数据库索引管理
  • JVM中的 -Xms参数 设置 JVM 的初始堆大小
  • Idea 创建 Spring 项目(保姆级)
  • C++多线程学习(一):C++11 多线程快速入门
  • Linux系统之lsof命令的基本使用
  • 性能压力测试的优势与重要性