当前位置:网站首页>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 .
边栏推荐
- Test picture
- 5G没能拉动行业发展,不仅运营商失望了,手机企业也失望了
- Explanation of common basic controls for C # form application (suitable for Mengxin)
- Kibana uses JSON document data
- As a VC, the auction house invested Web3 for the first time
- 对 int 变量赋值的操作是原子的吗?
- 1.3.1 Full Permutation Problem
- Rewrite the tensorrt version deployment code of yolox
- 【进程间通信IPC】- 信号量的学习
- Deep understanding of Kalman filter (2): one dimensional Kalman filter
猜你喜欢

苹果降价600元,对本就溃败的国产旗舰手机几乎是毁灭性打击

Five kinds of 3D attention/transformer finishing (a-scn, point attention, CAA, offset attention, point transformer)

5G没能拉动行业发展,不仅运营商失望了,手机企业也失望了

基于restful页面数据交互

CUDA Programming -03: thread level

ctfshow 终极考核

Explain cache consistency and memory barrier

Cross domain and processing cross domain

对 int 变量赋值的操作是原子的吗?

NiO Summary - read and understand the whole NiO process
随机推荐
BOM的常用操作和有关获取页面/窗口高度、宽度及滚动的兼容性写法
[flutter -- geTx] preparation
The execution sequence of async/await, macro tasks and micro tasks
Matlab uses m file to produce fuzzy controller
被三星和台积电挤压的Intel终放下身段,为中国芯片定制芯片工艺
ctfshow 终极考核
flex:1的原理
500 error reporting
ArkUI中的显式动画
网易笔试之解救小易——曼哈顿距离的典型应用
Interface test tool -postman usage details
Antdesign a-modal自定义指令实现拖拽放大缩小
基于restful页面数据交互
5g failed to stimulate the development of the industry, which disappointed not only operators, but also mobile phone enterprises
Principle of flex:1
二叉树讲解
Encountered 7 file(s) that should have been pointers, but weren‘t
Unity3d 2021 software installation package download and installation tutorial
linux安装和远程连接mysql记录
ArkUI框架中的两个小技巧