【Daily HarmonyOS Next Knowledge】 Getting App Package Name, Popup Background Color, Decorator Nesting Issues, Regex Problems, Text Component Max Width Setting
1. How to obtain the current app package name, active AbilityName, and active ModuleName in a HAR package in HarmonyOS?
The NFC card reading interface requires passing bundleManager.ElementName, which includes bundleName, abilityName, and moduleName. If the card reading logic is written in a HAR package, how to obtain these values?
import { MainPage } from 'harApp/Index';
import { common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
const TAG: string = 'testTag'
@Entry
@Component
struct OutPage {
@State message: string = 'OutPage';
private bundleName: string = ''
private abilityName: string = ''
private moduleName: string = ''
aboutToAppear(): void {
let context = getContext() as common.UIAbilityContext
hilog.info(0x0000, TAG, 'OutPage-bundleName: %{public}s', context.abilityInfo.bundleName)
hilog.info(0x0000, TAG, 'OutPage-abilityName: %{public}s', context.abilityInfo.name)
hilog.info(0x0000, TAG, 'OutPage-moduleName: %{public}s', context.abilityInfo.moduleName)
this.bundleName = context.abilityInfo.bundleName
this.abilityName = context.abilityInfo.name
this.moduleName = context.abilityInfo.moduleName
}
build() {
Column() {
MainPage({ bundleName: this.bundleName, abilityName: this.abilityName, moduleName: this.moduleName })
}
}
}
2. Does the HarmonyOS popup window have a built-in background color?
Currently, custom popups have a default blurred material. To set backgroundColor: Color.Transparent, also disable the blur effect with backgroundBlurStyle: BlurStyle.NONE.
3. When nesting second-level decorators in HarmonyOS, why isn't the list item control联动 (dynamically updated)?
Issues with @Observed and @ObjectLink decorators: Nesting class object properties doesn't trigger updates even with the same annotation format.
Cause: When assigning this.titleList = response.data?.form_info!!, the objects in titleList are not of the "Form_info" type, so @Observed can't monitor changes.
Solution: Use the third-party library class-transformer to convert data to the "Form_info" type:
this.titleList = plainToClass(Form_info, response.data?.form_info!!)
4. Why does TS code execute differently in VSCode and HarmonyOS ArkTS?
TS code behaves differently in VSCode and HarmonyOS ArkTS due to ArkTS's adaptation of regular expressions.
Reference: https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-more-cases-V5#arkts-no-regexp-literals
Example:
// Not recommended in ArkTS
let regex: RegExp = /\s*/g;
// Recommended
let regexp: RegExp = new RegExp('\\s*', 'g');
// Alternative for regex with flags
const combinedPattern = new RegExp("<[>]+>(?:[\s\S]*?)<\/[>]+>|\[[^\]]+\]", 'g');
5. How to set the maximum display width for a HarmonyOS Text component?
Demo:
@Entry
@Component
struct TextInputExample2 {
controller: TextInputController = new TextInputController()
@State text: string = ''
build() {
Column({ space: 20 }) {
Text('1111111111111111111111111111111'.split('').join('\u200B'))
.textOverflow({ overflow: TextOverflow.Ellipsis })
.maxLines(1)
.fontSize(12)
.border({ width: 1 })
.margin(100)
.constraintSize({ maxWidth: 200 })
}.width('100%')
}
}
Reference: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-size-V5
Top comments (0)