uniapp交互反馈

页面交互反馈可以通过:**uni.showToast(object)**实现,常用属性有

ioc值说明

值 说明
success 显示成功图标,此时 title 文本在小程序平台最多显示 7 个汉字长度,App仅支持单行显示。
error 显示错误图标,此时 title 文本在小程序平台最多显示 7 个汉字长度,App仅支持单行显示。
fail 显示错误图标,此时 title 文本无长度显示。
exception 显示异常图标。此时 title 文本无长度显示。
loading 显示加载图标,此时 title 文本在小程序平台最多显示 7 个汉字长度。
none 不显示图标,此时 title 文本在小程序最多可显示两

以下是不同值的效果图


除了使用系统提供的还可以使用自定义图标


案例代码

javascript 复制代码
<template>
	<view class="content">
		<button @click="showToast('success')">success</button>
		<button @click="showToast('error')">error</button>
		<button @click="showToast('fail')">fail</button>
		<button @click="showToast('exception')">exception</button>
		<button @click="showToast('loading')">loading</button>
		<button @click="showToast('none')">none</button>
		<button @click="myShowToast('自定义')">自定义</button>
	</view>
</template>

<script setup>
	var showToast = (str) => {
		uni.showToast({
			title: str,
			icon: str
		})
	}
	var myShowToast = (str) => {
	uni.showToast({
			title: str,
			image:'/static/img/2.png'
		})
	}
</script>

uni.showLoading与uni.hideLoading

注意事项

  1. 确保成对使用 : 确保每次调用 uni.showLoading 后都有相应的 uni.hideLoading 调用来隐藏加载提示框。

  2. 避免重复显示 : 如果在同一个异步操作中多次调用 uni.showLoading,可能会导致加载提示框无法正常隐藏。确保只在必要的地方调用。

通过这种方式,可以有效地使用 uni.showLoading 和 uni.hideLoading 来提高用户体验,确保用户在等待过程中得到及时反馈。


案例

javascript 复制代码
<template>
	<view>
		<button @click="showLoading()">showLoading</button>
		<button @click="hideLoading()">hideLoading</button>
	</view>
</template>

<script setup>
	var showLoading = () => {
		uni.showLoading({
			title: '加载中',
		})
	}
	var hideLoading = () => {
		uni.hideLoading()
	}
</script>

uni.showModal

显示模态弹窗,可以只有一个确定按钮,也可以同时有确定和取消按钮。类似于一个API整合了 html 中:alert、confirm

javascript 复制代码
<template>
	<view>
		<button @click="showModal()">showModal</button>
	</view>
</template>

<script setup>
	var showModal = () => {
		uni.showModal({
			title: '提示', // 弹窗标题
			content: '这是一个示例内容', // 弹窗主要信息
			confirmText: '确定', // 确定按钮的文字,默认为"确定"
			cancelText: '取消', // 取消按钮的文字,默认为"取消"
			success: function(res) {
				if (res.confirm) {
					console.log('用户点击确定');
					// 在这里处理用户点击确定后的逻辑
				} else {
					console.log('用户点击取消');
					// 在这里处理用户点击取消后的逻辑
				}
			},
			fail: function(err) {
				console.error('显示模态框失败:', err);
			}
		});

	}
</script>

参数说明

  • title: 弹窗标题,字符串类型。

  • content: 弹窗内容,字符串类型。

  • confirmText: 确认按钮文字,默认是"确定"。

  • cancelText: 取消按钮文字,默认是"取消"。

  • success: 接口调用成功的回调函数,参数 res 包含 confirm 和 cancel 两个属性,分别对应用户点击了确定和取消按钮。

  • fail: 接口调用失败的回调函数。


uni.showActionSheet

是一个用于在小程序或框架中弹出一个动作菜单的方法。这个方法接受一个配置对象 OBJECT,该对象包含了动作菜单的相关设置。

javascript 复制代码
uni.showActionSheet({
  itemList: ['选项1', '选项2', '选项3'], // 显示的按钮列表
  itemColor: '#007AFF', // 按钮的文字颜色
  cancelText: '取消', // 取消按钮的文字
  success: function(res) {
    if (res.tapIndex >= 0) {
      console.log('用户选择了第 ' + (res.tapIndex + 1) + ' 个选项');
      // 根据 res.tapIndex 执行相应操作
    }
  },
  fail: function(err) {
    console.error('显示操作菜单失败:', err);
  }
});

具体来说,uni.showActionSheet 的参数 OBJECT 包含以下属性:

  • itemList:数组类型,表示显示的按钮列表。
  • itemColor:字符串类型,表示按钮的文字颜色。
  • cancelText:字符串类型,表示取消按钮的文字。
  • success :回调函数,当用户选择某个选项时触发。回调函数会传递一个参数 res,其中包含用户选择的信息。
相关推荐
码间留白2 天前
六轮真机实验后,我把鸿蒙直播推流的 UI 全逼成了原生——uni-app 适配 HarmonyOS NEXT 完整复盘
uni-app·鸿蒙·harmonyos next·直播推流·arkts/arkui
wuxianda10304 天前
2026 苹果审核 4.3 (a) 被拒全解析|Guideline 4.3a spam 马甲包拒审原因 + 避坑方案
flutter·uni-app·苹果上架
头发还在的女程序员5 天前
基于 Spring Boot + Vue 3 + uni-app 的灵活用工系统:四端架构与业务流程设计
vue.js·spring boot·uni-app·灵活用工·灵工系统
Dumbness7 天前
订阅消息弹窗必须在用户点击里调:联调实验室里 timing 和 Mock 支付通知踩的坑
javascript·uni-app
码间留白8 天前
uni-app 鸿蒙离线打包:一个改动从源码到真机要穿过几条链
uni-app·鸿蒙·deveco·离线打包·uts插件
多客:duokelijie9 天前
代练订单系统选型思路:跨品类游戏接入、订单分层管理、自动结算逻辑拆解!
数据库·mysql·微信小程序·前端框架·uni-app·php·持续部署
码间留白9 天前
鸿蒙端对接 BLE 血压计:从一次握手到读数入库(二)设备篇
物联网·uni-app·鸿蒙·蓝牙ble·血压计
华玥作者10 天前
Vue 3 + TypeScript + uni-app 组件库推荐:hy-app(华玥组件库)
vue.js·typescript·uni-app
FinelyYang10 天前
uniapp使用kkfileview在线预览ppt,excel,word,pdf类型文件
pdf·uni-app·powerpoint
2501_9160088910 天前
iPhone 设备信息查看,用 KeyMob 看型号、序列号、电池健康和 UDID
android·ios·小程序·https·uni-app·iphone·webview