[Daily HarmonyOS Next Knowledge] Global Prompts, Text Size Changes, Tab Subpage Lifecycle, Routing Parameter Acquisition, State Monitoring
1. Does HarmonyOS support a global toast-like pop-up window that allows custom layouts and entry/exit animations?
Is there a pop-up window similar to a global toast that supports custom layouts and entry/exit animations?
Since the default toast has limited customization, it is recommended to use promptAction.openCustomDialog
to implement a transparent toast-like effect with custom layouts.
Reference Documentation: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-promptaction-V5#promptactionopencustomdialog11
openCustomDialog(options: CustomDialogOptions): Promise<number>
- Opens a custom pop-up window.
- Currently,
isModal = true
andshowInSubWindow = true
cannot be used simultaneously. - The pop-up width in portrait mode defaults to
window width - left/right margin (16vp, 40vp for 2-in-1 devices)
, with a maximum default width of 400vp.
2. How to convert the component size obtained via the onSizeChange
interface to pixels in HarmonyOS?
When obtaining the component size through the onSizeChange
interface, how to convert the sizeoptions
to pixels?
Reference for pixel unit conversion: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-pixel-units-V5
ArkUI provides 4 pixel units, with vp
as the base unit.
Name | Description |
---|---|
px | Physical screen pixel unit. |
vp | Density-related pixel unit, converted to physical pixels based on screen density. Default unit when no unit is specified. Note: The vp-to-px ratio depends on screen density. |
fp | Font pixel unit, similar to vp but adapts to system font size settings. |
lpx | Logical pixel unit relative to the viewport, calculated as actual screen width / logical width (configured by designWidth) . The default designWidth is 720. For a screen with 1440 physical pixels, 1lpx equals 2px when designWidth = 720 . |
3. When loading multiple @entry pages via Tab in HarmonyOS, why are the lifecycle methods onPageShow
and onPageHide
not called for tab subpages?
onPageShow
and onPageHide
are page-level lifecycle methods, but TabContent
is a subcomponent and does not trigger these lifecycles. To implement monitoring, use the @Watch
decorator: change the watched variable in the tab click callback and implement monitoring in the subcomponent.
Reference: https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-watch-V5
4. How to convert the type of results from getParamByName
in HarmonyOS?
When passing parameters between components via Navigation, getParamByName
returns Array<unknown>
. Even if a string is passed, type casting (e.g., as string
) fails.
Solution: Use String(XXX.getParamByName())
to cast the unknown type to a string.
5. In HarmonyOS state management, how can @watch
monitor a specific property within an observed class object?
Currently, @watch
can only monitor a single property. If there is a class A
decorated with @observe
, how to monitor only the a
property of A
?
Reference: https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-new-observedv2-and-trace-V5
To enhance state management for nested object properties, use the @ObservedV2
and @Trace
decorators:
-
@ObservedV2
and@Trace
enable direct monitoring of nested property changes, a core capability of State Management V2.
Top comments (0)