自定义showToast

<template>
  <view class="toast" v-if="visible">
    <view class="toast-content">
      <view v-if="r_icon" class="toast-icon">
        <icon :type="r_icon" color="white" size="40"/>
      </view>
      <view :class="r_icon ? 'toast-text-content' : ''">
        <text class="toast-text">{{ r_message }}</text>
      </view>
    </view>
  </view>
</template>
<script setup>
import {ref} from 'vue'

const icons = {
  success: 'success_no_circle',
  info: 'info',
  warn: 'warn',
  waiting: 'waiting',
  error: 'cancel',
  download: 'download',
  search: 'search',
  clear: 'clear',
  none: ''
}

const r_icon = ref('')
const visible = ref(false)
const r_message = ref('')
const showToast = (info) => {
  const {icon = 'none', title = '', duration = 2000} = info
  visible.value = true
  r_message.value = title
  r_icon.value = icons[icon]
  setTimeout(() => {
    visible.value = false
  }, duration)
}

defineExpose({
  showToast
})
</script>
<style scoped lang="scss">
.toast {
  left: 50%;
  top: 50%;
  padding: 20rpx;
  position: fixed;
  transform: translate(-50%, -50%);
  background-color: rgba(87, 87, 87, 0.9);
  box-shadow: 0 4px 8px rgba(0.1, 0.1, 0.1, 0.1);
  color: white;
  border-radius: 10rpx;
  z-index: 9999;

  .toast-content {
    display: block;
    align-items: center;
    justify-content: center;
    text-align: center;
    word-wrap: break-word;
    white-space: normal;
    max-width: 300rpx;


    .toast-text {
      white-space: normal;
      font-size: 26rpx;
      text-align: center;
    }

    .toast-text-content {
      margin-bottom: 20rpx;
    }

    .toast-icon {
      margin: 10rpx 0;
    }
  }
}
</style>
<template>
  <view>
    <button @click="toast">showToast</button>
  </view>
  <CustomToast ref="customToast"/>
</template>
<script setup>
import {ref} from 'vue'

const customToast = ref()
const toast = () => {
  return customToast.value.showToast({title: 'showToast~'})
}
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值