2***@qq.com
2***@qq.com
  • 发布:2025-04-10 16:30
  • 更新:2025-04-10 18:54
  • 阅读:222

【报Bug】uniapp vue3 uni.showToast 真机弹出后,不会消失

分类:uni-app

产品分类: uniapp/App

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: win10 2004

HBuilderX类型: 正式

HBuilderX版本号: 4.57

手机系统: Android

手机系统版本号: Android 9.0

手机厂商: 模拟器

手机机型: hc706

页面类型: vue

vue版本: vue3

打包方式: 云端

项目创建方式: HBuilderX

示例代码:

js文件 :
const toast = {
show(options) {
const defaultOptions = {
title: '',
icon: 'none',
duration: 2000,
mask: false
}
const toastOptions = Object.assign({}, defaultOptions, options)
// 处理预设类型
if (toastOptions.type) {
switch (toastOptions.type) {
case 'success':
toastOptions.icon = 'success'
toastOptions.image = '/static/image/success.png'
break
case 'error':
toastOptions.icon = 'error'
toastOptions.image = '/static/image/fial.png'
break
case 'loading':
toastOptions.icon = 'loading'
break
}
}
uni.showToast(toastOptions)
},
// 快捷方法
success(title, duration = 2000) {
this.show({
title,
type: 'success',
duration
})
},
error(title, duration = 2000) {
this.show({
title,
type: 'error',
duration
})
},
loading(title = '加载中...', duration = 60000) {
this.show({
title,
type: 'loading',
duration
})
},

hide() {  
    uni.hideToast()  
}  

}

export default toast

man.js 文件
app.config.globalProperties.$toast = toast

页面使用
tcBtn(){
this.$toast.success('测试弹窗')
},

操作步骤:

代码示例

预期结果:

代码示例

实际结果:

代码示例

bug描述:

uniapp vue3 uni.showToast 真机弹出后,不会消失

2025-04-10 16:30 负责人:无 分享
已邀请:
DCloud_UNI_JBB

DCloud_UNI_JBB

稍等我试下

  • 2***@qq.com (作者)

    不加image 是正常的,加了image 就会出现这个问题

    2025-04-10 16:34

DCloud_UNI_JBB

DCloud_UNI_JBB

把你的 type 改为 messageType 试试

show({  
    title,  
    messageType: 'success',  
    duration  
})
DCloud_UNI_JBB

DCloud_UNI_JBB

我这样写可以正常运行

function show(options) {  
    const defaultOptions = {  
        title: '',  
        icon: 'none',  
        duration: 2000,  
        mask: false,  
    }  
    const toastOptions = Object.assign({}, defaultOptions, options)  

    if (toastOptions.messageType) {  
        switch (toastOptions.messageType) {  
            case 'success':  
                toastOptions.icon = 'success'  
                toastOptions.image = '/static/logo.png'  
                break  
            case 'error':  
                toastOptions.icon = 'error'  
                toastOptions.image = '/static/logo.png'  
                break  
            case 'loading':  
                toastOptions.icon = 'loading'  
                break  
        }  
    }  

    uni.showToast(toastOptions)  
}  

// 快捷方法  
export function success(title, duration = 2000) {  
    show({  
        title,  
        messageType: 'success',  
        duration  
    })  
}
  • 2***@qq.com (作者)

    好的 我试试

    2025-04-10 17:35

  • DCloud_UNI_JBB

    回复 2***@qq.com: 我刚刚试了,没啥问题,你写的type属性,把showToast内部的type属性给覆盖了

    2025-04-10 17:44

DCloud_UNI_JBB

DCloud_UNI_JBB

const toast = {  
    show(options) {  
        const defaultOptions = {  
            title: '',  
            icon: 'none',  
            duration: 2000,  
            mask: false  
        }  
        const toastOptions = Object.assign({}, defaultOptions, options)  
        // 处理预设类型  
        if (toastOptions.messageType) {  
            switch (toastOptions.messageType) {  
                case 'success':  
                    toastOptions.icon = 'success'  
                    toastOptions.image = '/static/logo.png'  
                    break  
                case 'error':  
                    toastOptions.icon = 'error'  
                    toastOptions.image = '/static/logo.png'  
                    break  
                case 'loading':  
                    toastOptions.icon = 'loading'  
                    break  
            }  
        }  
        uni.showToast(toastOptions)  
    },  
    // 快捷方法  
    success(title, duration = 2000) {  
        this.show({  
            title,  
            messageType: 'success',  
            duration  
        })  
    },  
    error(title, duration = 2000) {  
        this.show({  
            title,  
            messageType: 'error',  
            duration  
        })  
    },  
    loading(title = '加载中...', duration = 60000) {  
        this.show({  
            title,  
            messageType: 'loading',  
            duration  
        })  
    },  
}
DCloud_UNI_JBB

DCloud_UNI_JBB

另外,关闭 loading 要主动关闭,参考文档 https://uniapp.dcloud.net.cn/api/ui/prompt.html#showloading

要回复问题请先登录注册