HarmonyOS 鸿蒙Next 示例代码中 Index.ets中获取context返回undefined

HarmonyOS 鸿蒙Next 示例代码中 Index.ets中获取context返回undefined

Stage模型,API version 9

DevEco Studio 3.1.1 Release

按照 “UIAbility组件与UI的数据同步”中示例代码,编写index.ets文件中事件触发,在entryAbility中监听事件并响应,

index.ets 代码如下:

import common from '@ohos.app.ability.common';
@Entry
@Component
struct Index {
  private context01 =  this as common.UIAbilityContext;
  
  onClickButton01() {
    console.log('onClickButton01');
    console.log(JSON.stringify(this));
    console.log(JSON.stringify(getContext(this)));
    this.context01.eventHub.emit('eventButton01OnClick');  //执行到这里报错:11200-11792 E C03900/Ace: [Engine Log]Error message: Cannot read property eventHub of undefined,添加了上面两个log,发现输出如下文
    console.log('onClickButton01 event emit');
  }
  
  /**
   * In low-code mode, do not add anything to the build function, as it will be
   * overwritten by the content generated by the .visual file in the build phase.
   */
  build() {}
}

在preview里点击按钮触发 onClickButton01() 报错,日志如下:

09-16 08:55:24.697 11200-11792 I A0c0d0/JSApp: app Log: onClickButton01
09-16 08:55:24.697 11200-11792 I A0c0d0/JSApp: app Log: {"childrenWeakrefMap_":{},"watchedProps":{},"dirtDescendantElementIds_":{},"updateFuncByElmtId":{},"id_":4,"providedVars_":{}}
09-16 08:55:24.697 11200-11792 I A0c0d0/JSApp: app Log: undefined

09-16 08:55:24.707 11200-11792 E C03900/Ace: [Engine Log]Lifetime: 0.000000s 09-16 08:55:24.707 11200-11792 E C03900/Ace: [Engine Log]Js-Engine: ark 09-16 08:55:24.707 11200-11792 E C03900/Ace: [Engine Log]page: pages

09-16 08:55:24.707 11200-11792 E C03900/Ace: [Engine Log]Error message: Cannot read property eventHub of undefined

09-16 08:55:24.707 11200-11792 E C03900/Ace: [Engine Log]Cannot get SourceMap info, dump raw stack:

09-16 08:55:24.707 11200-11792 E C03900/Ace: [Engine Log]Stacktrace:

09-16 08:55:24.707 11200-11792 E C03900/Ace: [Engine Log] at onClickButton01 (entry

通过日志发现:按示例代码 private context01 = getContext(this) as common.UIAbilityContext;

这里 getContext(this)undefined,请教下为啥呢?多谢~


更多关于HarmonyOS 鸿蒙Next 示例代码中 Index.ets中获取context返回undefined的实战教程也可以访问 https://www.itying.com/category-93-b0.html

4 回复

换成远程模拟器再试试看,预览器在内存、文件管理方面有很多功能是缺失的。

更多关于HarmonyOS 鸿蒙Next 示例代码中 Index.ets中获取context返回undefined的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


多谢,换了确实就没问题,

获取context放到方法里也不行,请教各位大佬是为啥呢?
```lua
import common from '[@ohos](/user/ohos).app.ability.common';
[@Entry](/user/Entry)
[@Component](/user/Component)
struct Index {
  //private context01 = getContext(this) as common.UIAbilityContext;
  onClickButton01() {
    let context01 = getContext(this) as common.UIAbilityContext; // 放进来也不行
    console.log('onClickButton01');
    console.log(JSON.stringify(this));
    console.log(JSON.stringify(getContext(this)));
    context01.eventHub.emit('eventButton01OnClick');
    console.log('onClickButton01 event emit');
  }

  /**
   * In low-code mode, do not add anything to the build function, as it will be
   * overwritten by the content generated by the .visual file in the build phase.
   */
  build() {}
}

在HarmonyOS鸿蒙Next中,Index.ets文件中获取context返回undefined,通常是因为context未正确初始化或未在正确的生命周期内获取。context是鸿蒙应用开发中的重要对象,用于访问应用资源和执行系统操作。以下是一些可能导致undefined的原因及解决方法:

  1. 生命周期问题context在组件的onInitonReady等生命周期方法中才能正确获取。确保在正确的生命周期方法中访问context,例如在onInit中获取:

    onInit() {
        let context = this.context; // 确保在onInit中获取
    }
    
  2. 组件未挂载:如果组件尚未挂载或未正确初始化,context可能为undefined。确保组件已正确加载并处于活动状态。

  3. 错误的this绑定:在事件处理函数或异步代码中,this可能未正确绑定,导致无法访问context。使用箭头函数或显式绑定this

  4. 示例代码问题:检查示例代码是否完整,是否存在遗漏或错误。确保所有依赖项和初始化步骤都已正确执行。

  5. API版本兼容性:鸿蒙系统版本或API可能影响context的获取。确保开发环境与示例代码兼容。

通过检查以上问题,可以有效解决context返回undefined的情况。

回到顶部