当前位置: 首页 > news >正文

微信小程序-自定义toast

微信小程序-自定义toast

    • 微信小程序原生的toast最多能显示两行文字。
    • 方案1:
    • 方案2

微信小程序原生的toast最多能显示两行文字。

在这里插入图片描述
有时候并不能满足业务需求。所以我们需要使用第三方或者自定义。

方案1:

第三方vant-toast
在这里插入图片描述
微信小程序下载引入第三方vant之后。
在需要使用的页面json文件里面引入

"usingComponents": {"van-toast": "@vant/weapp/toast/index"
}

wxml页面增加

<van-toast id="van-toast" />

js或者ts页面引入

var util = require('../../../utils/util')
import Toast from '@vant/weapp/toast/toast'Page({data: {},// 打开文档openDocumentClick(e: any) {console.log('下载URL', e)var loadUrl = e.currentTarget.dataset.key.urlwx.downloadFile({url: loadUrl, // 替换为你要下载的文件的 URL  success(res) {if (res.statusCode === 200) {console.log(res.tempFilePath)} else {Toast('下载失败' + res.errMsg);}},fail(err) {Toast('下载失败' + err);}});},})

以上亲测有效,可以显示多行提示文字。

方案2

自定义toast。在components里面
第一步:新建文件夹
第二步:新建component。
在这里插入图片描述

在这里插入图片描述
index.scss文件

.toast-container {position: fixed;top: 50%;left: 50%;transform: translate(-50%, -50%);z-index: 9999;
}.toast-content {background-color: rgba(0, 0, 0, 0.7);color: white;padding: 20rpx 30rpx;border-radius: 10rpx;display: flex;flex-direction: column;align-items: center;
}.toast-icon {width: 60rpx;height: 60rpx;margin-bottom: 10rpx;
}.toast-message {font-size: 28rpx;
}

index.wxml文件

<view class="toast-container" wx:if="{{show}}"><view class="toast-content"><image wx:if="{{icon}}" src="{{icon}}" class="toast-icon"></image><text class="toast-message">{{message}}</text></view>
</view>

index.ts文件

Component({properties: {message: String,duration: {type: Number,value: 3000},icon: {type: String,value: ''}},data: {show: false},methods: {showToast() {this.setData({ show: true })setTimeout(() => {this.hideToast()}, this.properties.duration)},hideToast() {this.setData({ show: false })}}
})

上面就是自定义的组件。接下来就是在需要使用的页面进行调用了。比如在首页home文件里面使用。
home.json

{ "navigationBarTitleText": "首页", "navigationStyle":"custom","navigationBarTextStyle":"white","usingComponents": {"van-popup": "@vant/weapp/popup/index","custom-toast": "/components/FT-Toast/index"}
}

home.wxml

<custom-toast id="toast" />

home.ts

var util = require('../../../utils/util')
import Toast from '@vant/weapp/toast/toast'Page({data: {},
showToast(message:any, icon = '') {this.selectComponent('#toast').setData({message,icon})this.selectComponent('#toast').showToast()},
// 打开文档openDocumentClick(e: any) {console.log('下载URL', e)var loadUrl = e.currentTarget.dataset.key.urlwx.downloadFile({url: loadUrl, // 替换为你要下载的文件的 URL  success(res) {if (res.statusCode === 200) {console.log(res.tempFilePath)} else {this.showToast('下载失败' + res.errMsg)}},fail(err) {this.showToast('下载失败' + err)}});},})

以上方案亲测有效。

以上两个方法都可以使用,个人建议直接使用vant里面的比较方便。

相关文章:

  • 最近准备写个Playbook,详细点的指导手册,作为后续的销售培训文件,也趁着这个机会整理下产品思路,尤其是对于UALink,UEC新的联盟规范的测试用例
  • 深入剖析 FastAdmin 权限验证体系:与 UserRule 表的紧密关联及最佳实践
  • 智能合约安全审计平台——可视化智能合约漏洞扫描
  • *差分自回归移动平均模型(ARIMA)
  • # 从零开发小红书风格Flutter应用:图片上传功能实现踩坑记录
  • Spark-SQL2
  • OpenCV中的轮廓近似方法详解
  • MCP Server 开发实战 | 大模型无缝对接 Grafana
  • 49、发起流式请求获取回答
  • Jarpress 开源项目重构公告
  • Java设计模式之观察者模式:从入门到架构级实践
  • 安卓基础(SQLite)
  • 设计模式之状态模式:优雅管理对象行为变化
  • 最简单的使用SDL2 播放原始音频数据程序
  • KrillinAI:视频跨语言传播的一站式AI解决方案
  • js原型链污染
  • 使用 LLaMA-Factory 对 DeepSeek R1进行微调教程
  • Docker--Docker镜像原理
  • PHP4 Session定制与使用指南
  • 部署LLaMA Factory,及快速使用
  • 普雷沃斯特当选新一任天主教罗马教皇
  • 云南临沧一行贿案金额认定比受贿案多41万,重审时检方变更金额起诉
  • 化学家、台湾地区“中研院”原学术副院长陈长谦逝世
  • 马上评|持续对标国际一流,才有22项“全球最优”
  • 现场|万米云端,遇见上博
  • 世界银行最新营商环境体检单:59个测评点,上海22项达全球最优水平