onWindowStageCreate 问题 - HarmonyOS 鸿蒙Next

onWindowStageCreate 问题 - HarmonyOS 鸿蒙Next

windowStage.loadContent(‘pages/Index’, (err, data) => { if (err) { hilog.error(0x0000, ‘testTag’, Failed to load the content. Code: ${err.code}, message: ${err.message}.); return; }

hilog.info(0x0000, ‘testTag’, Succeeded in loading the content. Data: ${JSON.stringify(data)}. ); });

这时候 data 不能确定是不是NULL值,程序会报错,应该怎么修改才能通过


更多关于onWindowStageCreate 问题 - HarmonyOS 鸿蒙Next的实战教程也可以访问 https://www.itying.com/category-93-b0.html

6 回复

去掉 下面引用没问题了

import { JSON } from '@kit.ArkTS';

更多关于onWindowStageCreate 问题 - HarmonyOS 鸿蒙Next的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


报错:
hvigor ERROR: Failed :MyShUbing:default@CompileArkTS...
hvigor ERROR: ArkTS Compiler Error
1 ERROR: ArkTS:ERROR File: D:/harmony/Shubing/MyShUbing/src/main/ets/myshubingability/MyShUbingAbility.ets:48:95
No overload matches this call.
Overload 1 of 2, '(value: Object, replacer?: (string | number)[], space?: string | number): string', gave the following error.
Argument of type 'void' is not assignable to parameter of type 'Object'.
Overload 2 of 2, '(value: Object, replacer?: Transformer, space?: string | number): string', gave the following error.
Argument of type 'void' is not assignable to parameter of type 'Object'.

引用

import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
import { JSON } from '@kit.ArkTS';

我的方法 提示
window.WindowStage.loadContent(path: string, callback: AsyncCallback<void, void>): void (+2 overloads)

试过可行?

在HarmonyOS中,onWindowStageCreate是一个生命周期回调方法,用于在窗口阶段创建时执行相关操作。该方法通常用于初始化UI组件、设置窗口属性或加载数据。具体实现如下:

onWindowStageCreate(windowStage: window.WindowStage) {
    // 设置窗口属性
    windowStage.loadContent('pages/Index', (err, data) => {
        if (err) {
            console.error('Failed to load the content. Cause: ' + JSON.stringify(err));
            return;
        }
        console.info('Succeeded in loading the content. Data: ' + JSON.stringify(data));
    });
}

在该方法中,windowStage参数表示当前窗口阶段,可以通过loadContent方法加载指定的页面内容。如果加载失败,会通过回调函数返回错误信息。

回到顶部