docs/api/loading.md
::: sourceCode
GitCode: https://gitcode.com/dcloud/uni-api/tree/alpha/uni_modules/uni-prompt
GitHub: https://github.com/dcloudio/uni-api/tree/alpha/uni_modules/uni-prompt
:::
显示 loading 提示框, 需主动调用 uni.hideLoading 才能关闭提示框。
它是一个悬浮弹出的、非组件内嵌的加载中提示。
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|---|
| 4.0 | 4.41 | 3.91 | 4.11 | 4.11 | 4.61 | 5.0 |
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| options | ShowLoadingOptions | 否 | - | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | uni.showLoading参数定义 |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| title | string | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 提示的内容,长度与 icon 取值有关。 |
| mask | boolean | 否 | - | Web: 4.0; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: x | 是否显示透明蒙层,防止触摸穿透,默认:false |
| success | (res: ShowLoadingSuccess) => void | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | uni.showLoading成功回调函数定义 |
| fail | (res: ShowLoadingFail) => void | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | uni.showLoading失败回调函数定义 |
| complete | (res: any) => void | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | uni.showLoading完成回调函数定义 |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errCode | number | 是 | - | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | uni.showLoading失败回调参数 |
| errSubject | string | 是 | - | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | 统一错误主题(模块)名称 |
| data | any | 否 | - | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | 错误信息中包含的数据 |
| cause | Error | 否 | - | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | 源错误信息,可以包含多个错误,详见SourceError |
| errMsg | string | 是 | - | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - |
::: sourceCode
GitCode: https://gitcode.com/dcloud/uni-api/tree/alpha/uni_modules/uni-prompt
GitHub: https://github.com/dcloudio/uni-api/tree/alpha/uni_modules/uni-prompt
:::
隐藏 loading 提示框。
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|---|
| 4.0 | 4.41 | 3.91 | 4.11 | 4.11 | 4.61 | 5.0 |
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| options | HideLoadingOptions | 否 | - | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | uni.showLoading参数定义 |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| loadingPage | UniPage | 否 | - | Web: 4.0; 微信小程序: x; Android: x; iOS: x; iOS uni-app x UTS 插件: x; HarmonyOS 系统版本: 12; HarmonyOS: x | 期望隐藏的目标LoadingPage 如果为null 会关闭当前栈顶全部LoadingPage |
| success | (res: HideLoadingSuccess) => void | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | uni.hideLoading成功回调函数定义 |
| fail | (res: HideLoadingFail) => void | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | uni.hideLoading失败回调函数定义 |
| complete | (res: any) => void | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | uni.hideLoading完成回调函数定义 |
| noConflict | boolean | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 需要基础库: 2.22.1 |
目前 toast 和 loading 相关接口可以相互混用,此参数可用于取消混用特性 |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errCode | number | 是 | - | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | uni.showLoading失败回调参数 |
| errSubject | string | 是 | - | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | 统一错误主题(模块)名称 |
| data | any | 否 | - | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | 错误信息中包含的数据 |
| cause | Error | 否 | - | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | 源错误信息,可以包含多个错误,详见SourceError |
| errMsg | string | 是 | - | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - |
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见 ::: preview https://hellouniappx.dcloud.net.cn/web/#/pages/API/loading/loading
appRedirect https://hellouniappx.dcloud.net.cn/appredirect.html?path=pages/API/loading/loading
示例
<template>
<view>
<page-head :title="data.title"></page-head>
<view class="uni-list">
<view class="uni-padding-wrap">
<view class="uni-title uni-common-mt">
<text class="uni-title-text"> 设置标题 </text>
</view>
</view>
<view class="uni-list uni-common-pl">
<radio-group @change="radioChange">
<radio class="uni-list-cell uni-list-cell-pd radio" v-for="(item, index) in data.items"
:key="item.value" :class="index < data.items.length - 1 ? 'uni-list-cell-line' : ''"
:value="item.value" :checked="index === data.current">
{{ item.name }}
</radio>
</radio-group>
</view>
</view>
<!-- <view class="uni-padding-wrap uni-common-mt" style="flex-direction: row; justify-content: space-between; align-items: center;">
<text class="uni-title-text"> iOS 雪花样式 </text>
<switch :checked="data.iosSpinner" @change="iosSpinnerChange" />
</view> -->
<view class="uni-padding-wrap">
<view class="uni-btn-v">
<button type="primary" @click="showLoading">显示 loading 提示框</button>
<button @click="hideLoading">隐藏 loading 提示框</button>
<text>为方便演示,loading弹出3秒后自动关闭</text>
</view>
</view>
<view class="uni-padding-wrap">
<text>{{data.callbackText}}</text>
<view class="uni-btn-v">
<button type="primary" @click="closeSomeLoading">关闭指定loading</button>
<button type="primary" @click="noParamLoading">无参数测试</button>
</view>
</view>
</view>
</template>
<script setup lang="uts">
type ItemType = {
value : string
name : string
}
type DataType = {
title : string;
items : ItemType[];
current : number;
callbackText : string[];
titleSelect : string;
iosSpinner : boolean;
}
const data = reactive({
title: 'loading',
items: [
{
value: 'null',
name: '无标题',
},
{
value: '三秒后自动关闭',
name: '普通标题',
},
{
value: '超长文本内容,测试超出范围-超长文本内容,测试超出范围-三秒后自动关闭',
name: '长标题',
},
],
callbackText: [] as Array<string>,
current: 0,
titleSelect: "null",
iosSpinner: true
} as DataType)
const jest_getWindowInfo = () : GetWindowInfoResult => {
return uni.getWindowInfo();
}
const radioChange = (e : UniRadioGroupChangeEvent) => {
const selected = data.items.find((item) : boolean => {
return item.value == e.detail.value
})
if (selected != null) {
data.titleSelect = selected.value
}
}
const iosSpinnerChange = (e : UniSwitchChangeEvent) => {
data.iosSpinner = e.detail.value
}
const hideLoading = () => {
uni.hideLoading();
}
const noParamLoading = () => {
uni.showLoading()
uni.showLoading({
success: function (showRet : ShowLoadingSuccess) {
data.callbackText.push("noParamLoading 1 success")
}
})
uni.showLoading({
complete: function (showRet : any) {
data.callbackText.push("noParamLoading 2 complete")
}
})
setTimeout(function () {
uni.hideLoading({
success: function (ret : HideLoadingSuccess) {
data.callbackText.push("hide loading success")
}
})
}, 2000)
}
/**
* 关闭指定loading
*/
const closeSomeLoading = () => {
const loading1 = uni.showLoading({
title: "第一个loading",
iosSpinner: data.iosSpinner,
success: function (res : ShowLoadingSuccess) {
data.callbackText.push("showLoading 1 success")
console.log("showLoading 1 success", res)
},
fail: function (res : ShowLoadingFail) {
data.callbackText.push("showLoading 1 fail")
console.log("showLoading 1 fail", res)
},
complete: function (res : any) {
data.callbackText.push("showLoading 1 complete")
console.log("showLoading 1 complete", res)
},
})
const loading2 = uni.showLoading({
title: "第二个loading",
iosSpinner: data.iosSpinner,
success: function (res : ShowLoadingSuccess) {
data.callbackText.push("showLoading 2 success")
console.log("showLoading 2 success", res)
},
fail: function (res : ShowLoadingFail) {
data.callbackText.push("showLoading 2 fail")
console.log("showLoading 2 fail", res)
},
complete: function (res : any) {
data.callbackText.push("showLoading 2 complete")
console.log("showLoading 2 complete", res)
},
})
setTimeout(function () {
uni.hideLoading({
loadingPage: loading2,
success: function (res : HideLoadingSuccess) {
data.callbackText.push("hideLoading 2 success")
console.log("hideLoading 2 success", res)
},
fail: function (res : HideLoadingFail) {
data.callbackText.push("hideLoading 2 fail")
console.log("hideLoading 2 fail", res)
},
complete: function (res : any) {
data.callbackText.push("hideLoading 2 complete")
console.log("hideLoading 2 complete", res)
},
})
}, 1000)
setTimeout(function () {
uni.hideLoading({
loadingPage: loading1,
success: function (res : HideLoadingSuccess) {
data.callbackText.push("hideLoading 1 success")
console.log("hideLoading 1 success", res)
},
fail: function (res : HideLoadingFail) {
data.callbackText.push("hideLoading 1 fail")
console.log("hideLoading 1 fail", res)
},
complete: function (res : any) {
data.callbackText.push("hideLoading 1 complete")
console.log("hideLoading 1 complete", res)
},
})
}, 3000)
}
const showLoading = () => {
console.log(data.titleSelect)
if (data.titleSelect == "null") {
uni.showLoading({
title: "",
iosSpinner: data.iosSpinner,
});
} else {
uni.showLoading({
title: data.titleSelect,
iosSpinner: data.iosSpinner,
});
}
setTimeout(() => {
hideLoading();
}, 3000);
}
onLoad(() => {
// uni.showLoading()
// setTimeout(() => {
// uni.hideLoading()
// }, 2000);
})
defineExpose({
data,
showLoading,
hideLoading,
closeSomeLoading,
noParamLoading,
})
</script>
:::
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errMsg | string | 是 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 错误信息 |