VUE3 + TS 封装全局Toast方法,支持自定义样式

实现代码:1.新增一个toast.ts文件
import success from '/@/assets/icon/toast_success.png'
/**
 * @author 苏秦
 * @param text 文案
 * @param icon 图标
 * @param time 存在时间
 */

const Toast = (text: number | string, icon?, time = 1.5) => {

    let toastTimeout: ReturnType<typeof setTimeout> | null = null; // 用于存储定时器的引用
    // 销毁定时器
    if (toastTimeout) {
        clearTimeout(toastTimeout);
    }

    // 检查是否存在现有的提示框,如果存在则移除
    const successToast = document.querySelector('.success_toast');
    if (successToast) {
        successToast.remove();
    }

    // 创建容器
    const div: HTMLDivElement = document.createElement('div')
    div.className = "success_toast"
    // 创建二级容器
    const toastContainer: HTMLDivElement = document.createElement('div')
    toastContainer.className = "success_toast_container"
    // 创建img
    if (icon) {
        if (icon == 'success') {
            icon = success
        } else if (icon == 'fail') {
            // icon = success
        }
        const img = document.createElement('img')
        img.src = icon
        img.className = 'success_toast_icon'
        toastContainer.appendChild(img)
    }
    //创建文本span
    const span = document.createElement('div')
    span.className = 'success_toast_text'
    span.innerHTML = text as string
    toastContainer.appendChild(span)
    div.appendChild(toastContainer)

    document.body.appendChild(div)
    toastTimeout = setTimeout(() => {
        div.remove()
    }, time * 1000)
}

export default Toast
 定义toast全局样式,可以在app.css中集中管理(注意z-index层级,可能会与组件样式层级冲突,我设置的是6000)
.success_toast {
  position: fixed;
  z-index: 6000;
  top: 45%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: rgb(0, 0, 0, 0.7);
  border-radius: 12px 12px 12px 12px;
  /* opacity: 0.7; */
}
.success_toast_container {
  max-width: 400px;
  padding: 25px 25px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.success_toast_icon {
  width: 34px;
  height: 34px;
  margin-right: 15px;
}
.success_toast_text {
  color: #ffffff;
  font-size: 30px;
  line-height: 39px;
  display: flex;
  justify-content: center;
  align-items: center;
}

然后就是在组件中引用与调用 Toast方法
 

// 引入Toast方法
import Toast from '/@/utils/toast'


// 调用Toast方法,支持三个参数(1:Toast文本,2:成功与失败,在方法内部判断显示对应的自定图标,3:Toast显示时长)
Toast('复制成功', 'success');
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在 UniApp 中封装一个 Toast 组件非常简单,可以按照以下步骤进行操作: 1. 创建一个新的 .vue 文件,用于封装 Toast 组件。 2. 在该文件中,定义一个 template(模板)来展示 Toast 内容,例如: ```html <template> <div class="toast">{{ message }}</div> </template> ``` 3. 在该文件中,使用 props 来接收 Toast 组件的参数,例如: ```javascript <script> export default { props: { message: String } } </script> ``` 4. 使用 CSS 样式来美化 Toast 组件的外观,例如: ```css <style scoped> .toast { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); padding: 10px 20px; background-color: rgba(0, 0, 0, 0.8); color: #fff; border-radius: 4px; } </style> ``` 5. 在需要使用 Toast 的页面或组件中,引入并注册 Toast 组件。例如,在 App.vue 中注册: ```javascript <template> <div> <toast :message="toastMessage" v-if="showToast"></toast> <!-- 其他内容 --> </div> </template> <script> import Toast from '@/components/Toast.vue' export default { components: { Toast }, data() { return { showToast: false, toastMessage: '' } }, methods: { showToast(message) { this.toastMessage = message this.showToast = true // 设置一定时间后关闭 Toast setTimeout(() => { this.showToast = false }, 2000) } } } </script> ``` 6. 在需要弹出 Toast 的地方,调用 showToast 方法,并传入相应的消息: ```javascript this.showToast('Hello, UniApp!') ``` 这样,你就成功地封装了一个简单的 Toast 组件,在需要时可以随时调用并显示相应的提示信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值