在HarmonyOS NEXT开发中打开全局弹窗报错?

在HarmonyOS NEXT开发中打开全局弹窗报错?在普通类中打开全局弹窗

Reason:TypeError 
Error name:TypeError 
Error message:is not callable 
SourceCode: 
  (parent ? parent : this).observeComponentCreation2((elmtId, isInitialRender) => { 
    ^
阅读 540
avatarAI BotBETA

回答

在 HarmonyOS NEXT 开发中,如果你遇到在普通类中打开全局弹窗时报错,特别是 TypeError: is not callable 的错误,这通常是因为你尝试调用的方法或函数实际上并不存在或者没有正确引用。

错误信息显示 (parent ? parent : this).observeComponentCreation2 不是一个可调用的函数。这可能是因为:

  1. 方法名错误observeComponentCreation2 可能不是一个有效的 API。请检查 HarmonyOS 的官方文档,确认正确的方法名。
  2. 上下文错误:确保你在正确的上下文或对象上调用该方法。在 HarmonyOS 中,UI 操作通常需要在特定的 UI 组件或页面的上下文中进行。
  3. 权限或配置问题:全局弹窗可能需要特定的权限或配置。请检查你的项目配置,确保已正确设置必要的权限。

解决方案

  • 确认方法名:查阅最新的 HarmonyOS 开发文档,找到正确的方法来打开全局弹窗。
  • 使用正确的上下文:确保你在一个有效的 UI 组件或页面的实例上调用弹窗方法。
  • 检查权限和配置:确保你的应用具有显示全局弹窗所需的权限,并且已正确配置。

如果你正在尝试在普通类中(非 UI 组件类)执行 UI 操作,考虑将相关代码移至 UI 组件类或适当的事件处理程序中。

1 个回答

参考demo:
OpenDialogDemo .ets

import { TestDialog } from './DialogHelper'; 
 
@Entry 
@Component 
struct OpenDialogDemo { 
  build() { 
    Column() { 
      Text('点击弹窗') 
        .margin({ top: 25 }) 
        .fontSize(25) 
        .onClick(()=>{ 
          TestDialog(); 
        }) 
    } 
  } 
}

TestDialog.ets

import { ComponentContent, window } from '@kit.ArkUI'; 
import { BusinessError } from '@kit.BasicServicesKit'; 
import { openOcrBottomDialog } from './OpenOcrBottomDialog'; 
 
export function TestDialog(){ 
  let windowClass = AppStorage.get("windowStage") as window.WindowStage; 
  let uiContext = windowClass.getMainWindowSync().getUIContext(); 
  let promptAction = uiContext.getPromptAction(); 
  let contentNode = new ComponentContent(uiContext, wrapBuilder(openOcrBottomDialog)); 
  try { 
    AppStorage.setOrCreate("contentNode",contentNode) 
    promptAction.openCustomDialog(contentNode) 
  } catch (error) { 
    let message = (error as BusinessError).message; 
    let code = (error as BusinessError).code; 
    console.error(`OpenCustomDialog args error code is ${code}, message is ${message}`); 
  } 
}

openOcrBottomDialog.ets

import { BusinessError } from '@kit.BasicServicesKit' 
import { window } from '@kit.ArkUI' 
 
@Builder 
export function openOcrBottomDialog() { 
  Column() { 
    Text('我是内容').fontSize(20).margin({ top: 10, bottom: 10 }) 
 
    Image("") 
      .height(1) 
      .width('100%') 
      .margin({ left: 10, right: 10 }) 
      .backgroundColor(0xDFDFDF) 
 
    Text('拍照') 
      .fontSize(20) 
      .margin({ top: 10, bottom: 10 }) 
      .onClick(() => { 
        let sasasa: ComponentContent = AppStorage.get("contentNode") as ComponentContent 
        console.log("测试内容中contentNode= " + sasasa) 
        try { 
          let windowClass = AppStorage.get("windowStage") as window.WindowStage; 
          let uiContext = windowClass.getMainWindowSync().getUIContext(); 
          let promptAction = uiContext.getPromptAction(); 
          promptAction.closeCustomDialog(sasasa); 
        } catch (error) { 
          let message = (error as BusinessError).message; 
          let code = (error as BusinessError).code; 
          console.error(`:::OpenCustomDialog args error code is ${code}, message is ${message}`); 
        }; 
      }) 
 
    Image("") 
      .height(1) 
      .width('100%') 
      .margin({ left: 10, right: 10 }) 
      .backgroundColor(0xDFDFDF) 
 
    // Text('从相册获取') 
    // .fontSize(20) 
    // .margin({ top: 10, bottom: 10 }) 
    // .onClick(() => { 
    // // this.controller.close() 
    // promptAction.closeCustomDialog(contentNode); 
    // }) 
 
    Image("") 
      .height(1) 
      .width('100%') 
      .margin({ left: 10, right: 10 }) 
      .backgroundColor(0xDFDFDF) 
 
    // Text('取消') 
    // .fontSize(20) 
    // .margin({ top: 10, bottom: 10 }) 
    // .onClick(() => { 
    // // this.controller.close() 
    // promptAction.closeCustomDialog(contentNode); 
    // }) 
  } 
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进