[Daily HarmonyOS Next Knowledge] Object Undefined Issue, Object Non-Settable, String Encoding/Decoding, Parameters for Navigating to Existing Pages, Animation Handling
1. HarmonyOS Error: "Object is possibly 'undefined'"?
The error persists even after checking if a variable is empty.
Refer to the following syntax:
if (this.port_pair_map !== undefined && this.port_pair_map.get("123") !== undefined) {
const arr: webview.WebMessagePort[] = this.port_pair_map.get("123") as webview.WebMessagePort[];
// arr[1].postMessageEvent("11111");
(this.port_pair_map.get("123") as webview.WebMessagePort[])[1].postMessageEvent("1111");
}
2. HarmonyOS Error: "Cannot set property when setter is undefined"?
aboutToReuse(params: ReusableParams): void { // { ui: ThemeCard, isEnabling: boolean }
Log.error(TAG, 'aboutToReuse: ' + this.ui?.title + ', params:' + JSON.stringify(params));
if (params !== undefined && params.ui !== undefined) {
this.ui = params?.ui as ThemeCard;
Log.error(TAG, 'aboutToReuse ------ : ' + this.ui.getUnlockWallpaperUrl());
}
}
An error occurs when assigning a value to this.ui
.
Explanation:
This error typically occurs when trying to set a value for a property without a setter method. This might happen if you're trying to modify a read-only property or a property lacking a setter.
Solution:
Check your code for properties without setter methods and add setters where necessary.
3. How to perform Base64 encoding and decoding for strings in HarmonyOS?
Convert via buffer
first. Refer to: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-util-V5
@ohos.util (Utility Functions)
This module provides common utility functions, including string encoding/decoding (TextEncoder, TextDecoder), rational number operations (RationalNumber), buffer management (LRUCache), range checking (ScopeHelper), Base64 encoding/decoding (Base64Helper), built-in object type checking (types), and method interception/replacement (Aspect).
4. How to close a specific page or update parameters when navigating to an existing page in HarmonyOS?
Scenario: A reading page A exists in memory. Navigating from page B to page A (unique in the stack) requires updating page A's parameters with data from page B.
Solution: Use the @Provide
and @Consume
decorators for two-way component synchronization.
5. HarmonyOS Animation Effects + Gestures?
Official Documentation:
Edge Effects: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-scroll-V5#edgeeffect
edgeEffect(edgeEffect: EdgeEffect, options?: EdgeEffectOptions)
Sets edge sliding effects.Calendar Swipe Gestures: Refer to drag gesture usage in the official documentation:
https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-gestures-pangesture-V5
Top comments (0)