【微信小程序】自定义showToast

对于wx小程序自己提供的showToast,当遇到提示的信息字数较多时,小程序中showToast默认只能显示7个字体长度,因此多余的字数会隐藏,所以我们就自己定义一个弹出框解决这个问题。

toast组件:

<template>
  <view class="container" v-if="Flag">
    <view class="content">
      {{ content }}
    </view>
  </view>
</template>

<script>
 //定时器标识timer
var timer = null;
export default {
  name: "mytoast",
  data() {
    return {
      Flag: false,
      content: "",
    };
  },
  methods: {
    showtoast(val) {
      this.content = val;
      // 每次清除上一次timer,若无也没影响
      clearTimeout(timer);
      this.Flag = true;
       // 添加定时器,3秒后消失
      timer = setTimeout(() => {
        this.Flag = false;
      }, 3000);
    },
  },
};
</script>

<style scoped>
.container {
  color: #ffffff;
  padding: 20rpx 20rpx;
  background: rgba(0, 0, 0, 0.6);
  border-radius: 4px;
  position: absolute;
  left: 50%;
  top: 75%;
  transform: translate(-50%, -50%);
}

.content {
  text-align: center;
  font-size: 28rpx;
  width: 100%;
  height: 100%;
}
</style>

如何使用:

在父组件中需要使用组件的地方加入:

使用:<toast ref="mytoast"></toast>

引入:import toast from "./toast";

注册:components: { toast },

btnClick() {
	  // 绑定toast组件,调用toast组件内的方法
	  this.$refs.mytoast.showtoast( '成功' )
   }

效果:

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序中的showToast是一个API,用于显示一个弹出提示框。它接受一些参数,可以自定义提示文字、显示时长、图标等。在引用中的代码中,showToast的参数设置为{ title: '已取消本次操作', icon: 'success', duration: 2000 },表示显示一个成功的提示框,提示文字为“已取消本次操作”,显示时长为2000毫秒。引用和引用中的代码也是使用showToast来显示不同的提示。但是在引用中遇到了问题,因为showToast和showLoading调用了同一个框,导致冲突。所以在使用showToast时,需要注意避免与其他API冲突。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [微信小程序:消息提示框(wx.showToast)和 交互提示框(wx.showModal)](https://blog.csdn.net/Caesar6666/article/details/105810633)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.null"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [解决微信小程序真机 showToast 不显示](https://download.csdn.net/download/weixin_38749268/16209108)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.null"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [微信小程序 详解弹框wx.showToast、wx.showModal、wx.showActionSheet,modal控件解释及input输入框值的...](https://blog.csdn.net/yingtian648/article/details/80004334)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.null"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值