HarmonyOS 鸿蒙Next 全局loading
HarmonyOS 鸿蒙Next 全局loading
全局loading 
问下网络请求全局的loading界面怎么处理。CustomDialog对话框是需要在@Entry里面定义的。
        
          3 回复
        
      
      
        可以参考以下demo: 1、新建GlobalContext.ets工具类
export class GlobalContext {
  private constructor() {
  }
  private static instance: GlobalContext;
  private _objects = new Map<string, Object>();
  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
    }
    return GlobalContext.instance;
  }
  getObject(value: string): Object | undefined {
    return this._objects.get(value);
  }
  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }
}
2.新建DialogUtils.ets工具类
import { promptAction } from '@kit.ArkUI'
import { GlobalContext } from './GlobalContext'
let customDialogId: number = 0
@Builder
export function customDialogBuilder(content: String) {
  Column() {
    Text(`${content}` ).fontSize(20).height("30%")
    Text('失败原因:失败原因失败原因失败原因失败原因失败原因失败原因失败原因!').fontSize(16).height("30%")
    Row() {
      Button("确认").onClick(() => {
        promptAction.closeCustomDialog(customDialogId)
      })
      Blank().width(50)
      Button("取消").onClick(() => {
        promptAction.closeCustomDialog(customDialogId)
      })
    }
    .margin({ top: 30 })
  }.height(200).padding(5)
}
export function testPromptDialog() {
  const that = GlobalContext.getContext().getObject('UIContext') as UIContext;
  if (that) {
    promptAction.openCustomDialog({
      builder: customDialogBuilder.bind(that, "网络请求失败!")
    }).then((dialogId: number) => {
      customDialogId = dialogId;
    })
  }
}
3.页面入口调用
import { GlobalContext } from '../uitls/GlobalContext'
import { testPromptDialog } from '../uitls/DialogUtils'
@Entry
@Component
struct Index {
  aboutToAppear(): void {
    GlobalContext.getContext().setObject('UIContext', this)
  }
  build() {
    Row() {
      Column() {
        Button("promptAction弹窗")
          .onClick(() => {
            testPromptDialog()
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
更多关于HarmonyOS 鸿蒙Next 全局loading的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
Navigation 实现的路由,直接就根组件上和app视图同级做所有的全局类UI就行了啊
针对HarmonyOS 鸿蒙Next全局loading的问题,这通常与系统的资源分配、网络请求处理或UI渲染机制有关。以下是一些可能的解决方案:
- 系统资源检查:确保设备有足够的CPU和内存资源来支持全局loading的显示。过多的后台应用或服务可能会占用资源,导致loading显示异常。
 - 网络请求优化:如果全局loading与网络请求相关,检查请求的超时设置是否合理,服务器响应速度是否正常。优化网络请求的代码实现,确保使用高效的网络库和正确的异步处理机制。
 - UI渲染机制:检查UI的渲染逻辑,确保在需要显示loading时,相关的UI元素能够正确渲染。避免在极短时间内频繁创建或销毁loading弹窗,这可能导致UI显示问题。
 - 日志分析:利用鸿蒙系统提供的日志工具,分析全局loading显示过程中的日志信息,查找可能的错误或异常。
 
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html 。
        
      
                  
                  
                  
