在写一个简单的页面文档中,我们只写了一个简单的静态页面,现在我们来实现一个动态的页面。
创建第二个页面
打开我们创建好的sample_demo项目并展开目录。
方法一
- 进入entry/src/main/ets/pages目录,点击pages选中后右键-新建-Page

- 输入SecondPage,点击 Finish ,完成创建第二个新页面

- 页面自动填充模板代码

方法二
- 进入entry/src/main/ets/pages目录,点击pages选中后右键-新建-文件

- 输入SecondPage.ets,回车后完成页面创建

- 进入entry/src/main/resources/base/profile目录,在main_pages.json文件中的“src”下配置第二个页面的路由“pages/Second”。示例如下:
{
"src": [
"pages/Index",
"pages/Second"
]
}
- 配置完成路由后,就已经完成第二个页面的创建,但此时SecondPage.ets是空白的,需要开发者自行编写代码。
添加文本及按钮
参照Index.ets页面,在SecondPage.ets页面中添加Text组件、Button组件等,并设置其样式。SecondPage.ets示例如下:
@Entry
@Component
struct SecondPage {
@State message: string = 'Hi there';
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
Button() {
Text('Back')
.fontSize(25)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.margin({
top: 20
})
.backgroundColor('#0D9FFB')
.width('40%')
.height('5%')
}
.width('100%')
}
.height('100%')
}
}
实现页面之间的跳转
页面间的导航可以通过页面路由router来实现。页面路由router根据页面url找到目标页面,从而实现跳转。使用页面路由请导入router模块。
如果需要实现更好的转场动效等,推荐使用Navigation。
-
第一个页面跳转到第二个页面。
在第一个页面中,跳转按钮绑定onClick事件,点击按钮时跳转到第二页。Index.ets文件的示例如下:
import router from '@ohos.router';
import { BusinessError } from '@ohos.base';
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
Button() {
Text('Next')
.fontSize(30)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.margin({
top: 20
})
.backgroundColor('#0D9FFB')
.width('40%')
.height('5%')
.onClick(() => {
console.info(`Succeeded in clicking the 'Next' button.`)
router.pushUrl({ url: 'pages/SecondPage' }).then(() => {
console.info('Succeeded in jumping to the second page.')
}).catch((err: BusinessError) => {
console.error(`Failed to jump to the second page.Code is ${err.code}, message is ${err.message}`)
})
})
}
.width('100%')
}
.height('100%')
}
}
-
第二个页面返回到第一个页面。
在第二个页面中,返回按钮绑定onClick事件,点击按钮时返回到第一页。SecondPage.ets文件的示例如下:
import router from '@ohos.router';
import { BusinessError } from '@ohos.base';
@Entry
@Component
struct SecondPage {
@State message: string = 'Hi there';
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
Button() {
Text('Back')
.fontSize(25)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.margin({
top: 20
})
.backgroundColor('#0D9FFB')
.width('40%')
.height('5%')
.onClick(() => {
console.info(`Succeeded in clicking the 'Back' button.`)
try {
router.back()
console.info('Succeeded in returning to the first page.')
} catch (err) {
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`Failed to return to the first page.Code is ${code}, message is ${message}`)
}
})
}
.width('100%')
}
.height('100%')
}
}
推送应用到真机运行 ,查看实际效果。
@State和private的使用
在上面的示例中,我们已经用到了@State,在KaihongOS中,@State 和 private 是用于状态管理和访问控制的两个不同的概念,在开发过程中起到了很重要的作用,接下来我们简单了解一下它们的用途和特点。
@State
@State 是一个装饰器,用于声明组件内的状态变量。这些变量通常用于存储组件的内部状态,如用户交互的结果或临时数据。当@State变量的值发生变化时,会自动触发组件的重新渲染,以更新UI。@State变量具有以下特点:
- 内部私有: @State变量只能在组件内部访问,这保证了组件状态的封装性。
- 数据独立: 不同实例的@State变量之间数据是独立的,即每个组件实例都有自己的状态副本。
- 必须本地初始化: 所有@State变量必须在声明时初始化,以确保框架行为的确定性。
private
private 是一个访问限定符,用于限制变量或函数的可见性,使其只能在声明它的组件或类内部访问。使用private可以隐藏组件的内部实现细节,防止外部直接访问或修改内部状态,从而保护组件的状态不被意外改变。private的特点包括:
- 封装性: private变量或函数只能在声明它们的组件或类内部使用,这有助于封装组件的内部逻辑。
- 安全性: 通过限制访问,private有助于防止外部代码意外或恶意地修改组件的内部状态。
注: 当前文档中的源码路径:entry/src/main/ets/pages/dynamicPage目录下的Index.ets和SecondPage.ets