当前位置:网站首页>Two tips in arkui framework
Two tips in arkui framework
2022-07-27 09:08:00 【Hua Weiyun】
app.ets Define global objects in , Among others eTS How to obtain files
I believe everyone has such a question , How to be in app.ets Define global objects in ( Global variables 、 Method ), How else ets File and apply it ?
stay js Mode in app.js Objects are defined in , You can customize js Through the file getApp() obtain app.js The objects exposed in the game .
So here's the problem : stay app.ets Next , How to achieve it ?
in addition , When app.ets When the value of the global variable in changes , Will other referenced interfaces refresh automatically ? How to do ?
Actually FA Model USES globalThis It can realize global variable sharing .
Let's see the effect first :
Now let's take a look at , How did we achieve .
app.ets in :
export default { onCreate() { console.info('Application onCreate') globalThis.title = 'OpenHarmony' }, onDestroy() { console.info('Application onDestroy') },}index.ets
@[email protected] Global { @State title: string = '' aboutToAppear(){ this.title = globalThis.title } build() { Row() { Column() { Text(this.title) .fontSize(50) .fontWeight(FontWeight.Bold) } .width('100%') } .height('100%') }}It should be noted that globalThis Must be consistent , Otherwise, you won't get value .
This also gives us some enlightenment , We can use it to deal with the overall situation , Some common things can be encapsulated here , Global use
ets How to encapsulate code
stay Flutter Such problems are often encountered in . If you don't package some common components , Then your code will be very verbose , And it's not easy to read , At this time, we need to extract
that , How to extract ?
@[email protected] Global { @State title: string = '' aboutToAppear(){ this.title = globalThis.title } @Builder CustomTitle(text:string) { Text(text) .fontSize(16) .fontWeight(FontWeight.Bold) .margin({ top: 10, bottom: 10 }) } build() { Row() { Column() { this.CustomTitle('1、 come on. :') this.CustomTitle('2、 Strive :') Text(this.title) .fontSize(50) .fontWeight(FontWeight.Bold) } .width('100%') } .height('100%') }}adopt @Builder CustomTitle To encapsulate .
that , How to extract the same method ?
First define
// Text Public style @Extend(Text) function commonStyle () { .width('100%') .fontSize(14)} Column() { Text('abc,').commonStyle().textCase(TextCase.LowerCase) Text('abc').commonStyle().textCase(TextCase.UpperCase) }Reuse .
边栏推荐
- 5G没能拉动行业发展,不仅运营商失望了,手机企业也失望了
- Hangzhou E-Commerce Research Institute released an explanation of the new term "digital existence"
- linux下安装oracle,本地PL/SQL连接Linux下的oracle导入表并新建用户和密码
- 如何在B站上快乐的学习?
- [flutter -- geTx] preparation
- Huawei machine test question: String transformation minimum string JS
- B tree
- 拍卖行做VC,第一次出手就投了个Web3
- [interprocess communication IPC] - semaphore learning
- Huawei machine test question: Martian computing JS
猜你喜欢
随机推荐
Deep understanding of Kalman filter (1): background knowledge
Unity3d 2021 software installation package download and installation tutorial
Some practical, commonly used and increasingly efficient kubernetes aliases
Redis network IO
How to optimize the deep learning model to improve the reasoning speed
flex:1的原理
接口测试工具-Jmeter压力测试使用
[I2C reading mpu6050 of Renesas ra6m4 development board]
D3.v3.js data visualization -- pictures and tips of force oriented diagram
08_ Service fusing hystrix
qt中使用sqlite同时打开多个数据库文件
As a VC, the auction house invested Web3 for the first time
如何在B站上快乐的学习?
函数防抖节流
Cross domain and processing cross domain
B tree
ArkUI框架中的两个小技巧
Detailed explanation of two methods of Sqlalchemy
Low cost, low threshold, easy deployment, a new choice for the digital transformation of 48 million + small and medium-sized enterprises
Mmrotate trains its dataset from scratch







