
@CustomDialog
@Component
struct LoadingDialogExample {
controller?: CustomDialogController;
build() {
Row() {
LoadingProgress()
.width('40%')
.height('100%')
.margin({ left: 10 })
Text('加载中')
.fontSize(20)
.width('40%')
.height('100%')
.textAlign(TextAlign.Start)
.margin({ left: 2 })
}
.width('100%')
.height('100%')
}
}
@Entry
@Component
struct Index {
@State content: string = '';
dialogController: CustomDialogController = new CustomDialogController({
builder: LoadingDialogExample(),
width: 250,
height: 60,
cornerRadius: 10
})
async aboutToAppear(): Promise<void> {
this.dialogController.open();
await loadForeData().then((data) => {
this.content = data;
}); // 加载前端数据
await myAsyncFunction(); // 加载后端数据
this.dialogController.close();
}
build() {
Column() {
Text(this.content)
.fontSize(20)
.height('10%')
.width('100%')
.textAlign(TextAlign.Center)
}
.height('100%')
.width('100%')
}
}
async function myAsyncFunction(): Promise<void> {
const result: string = await new Promise((resolve: Function) => {
// 模拟加载数据
setTimeout(() => {
resolve('Hello, world!');
}, 3000);
});
}
async function loadForeData(): Promise<string> {
const result: string = await new Promise((resolve: Function) => {
// 模拟加载数据
setTimeout(() => {
resolve('Hello, world!');
}, 1000);
});
return '测试页面';
} 加载完成前台数据后及时关闭加载弹窗,不使用await等待后台数据加载完成。
@CustomDialog
@Component
struct LoadingDialogExample {
controller?: CustomDialogController;
build() {
Row() {
LoadingProgress()
.width('40%')
.height('100%')
.margin({ left: 10 });
Text('加载中')
.fontSize(20)
.width('40%')
.height('100%')
.textAlign(TextAlign.Start)
.margin({ left: 2 });
}
.width('100%')
.height('100%');
}
}
@Entry
@Component
struct Dialog {
@State content: string = '';
dialogController: CustomDialogController = new CustomDialogController({
builder: LoadingDialogExample(),
width: 250,
height: 60,
cornerRadius: 10
});
async aboutToAppear(): Promise<void> {
this.dialogController.open();
await loadForeData().then((data) => {
this.content = data;
});
myAsyncFunction();
this.dialogController.close();
}
build() {
Column() {
Text(this.content)
.fontSize(20)
.height('10%')
.width('100%')
.textAlign(TextAlign.Center);
}
.height('100%')
.width('100%');
}
}
async function myAsyncFunction(): Promise<void> {
const result: string = await new Promise((resolve: Function) => {
// 模拟加载数据
setTimeout(() => {
resolve('Hello, world!');
}, 3000);
});
console.info(result);
}
async function loadForeData(): Promise<string> {
const result: string = await new Promise((resolve: Function) => {
// 模拟加载数据
setTimeout(() => {
resolve('Hello, world!');
}, 1000);
});
console.info(result);
return '测试页面';
} 效果图如下:
