微信小程序-自定义toast

发布于:2025-04-17 ⋅ 阅读:(23) ⋅ 点赞:(0)

微信小程序原生的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.url
    wx.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.url
    wx.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里面的比较方便。


网站公告

今日签到

点亮在社区的每一天
去签到