当前位置:首页 > VUE

vue 实现toast组件

2026-01-16 04:09:22VUE

实现 Vue Toast 组件的方法

创建基础 Toast 组件

在 Vue 项目中创建一个基础 Toast 组件,通常包含消息内容和显示控制逻辑。

<template>
  <div v-if="visible" class="toast">
    {{ message }}
  </div>
</template>

<script>
export default {
  name: 'Toast',
  data() {
    return {
      visible: false,
      message: ''
    }
  },
  methods: {
    show(message, duration = 2000) {
      this.message = message
      this.visible = true
      setTimeout(() => {
        this.visible = false
      }, duration)
    }
  }
}
</script>

<style>
.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  padding: 10px 20px;
  background-color: rgba(0, 0, 0, 0.7);
  color: white;
  border-radius: 4px;
  z-index: 9999;
}
</style>

全局注册 Toast 服务

将 Toast 组件注册为全局服务,方便在任何组件中调用。

// src/plugins/toast.js
import Vue from 'vue'
import Toast from '@/components/Toast.vue'

const ToastConstructor = Vue.extend(Toast)
let toastInstance = null

function showToast(message, duration) {
  if (!toastInstance) {
    toastInstance = new ToastConstructor({
      el: document.createElement('div')
    })
    document.body.appendChild(toastInstance.$el)
  }
  toastInstance.show(message, duration)
}

export default {
  install(Vue) {
    Vue.prototype.$toast = showToast
  }
}

在 main.js 中安装插件

// src/main.js
import Vue from 'vue'
import Toast from '@/plugins/toast'

Vue.use(Toast)

在组件中使用 Toast

// 在任何组件中调用
this.$toast('这是一条提示消息', 3000)

添加动画效果

为 Toast 添加淡入淡出动画效果。

.toast {
  /* 其他样式 */
  opacity: 0;
  transition: opacity 0.3s ease;
}

.toast.show {
  opacity: 1;
}

修改组件逻辑支持动画

methods: {
  show(message, duration = 2000) {
    this.message = message
    this.visible = true
    this.$nextTick(() => {
      this.$el.classList.add('show')
      setTimeout(() => {
        this.$el.classList.remove('show')
        setTimeout(() => {
          this.visible = false
        }, 300)
      }, duration)
    })
  }
}

支持多种类型

扩展 Toast 支持 success、error、warning 等不同类型。

<template>
  <div v-if="visible" :class="['toast', `toast-${type}`]">
    <span class="toast-icon">{{ iconMap[type] }}</span>
    {{ message }}
  </div>
</template>

<script>
export default {
  data() {
    return {
      type: 'info',
      iconMap: {
        info: 'ℹ️',
        success: '✅',
        warning: '⚠️',
        error: '❌'
      }
    }
  },
  methods: {
    show(message, type = 'info', duration = 2000) {
      this.type = type
      this.message = message
      this.visible = true
      // 其他逻辑
    }
  }
}
</script>

<style>
.toast-success {
  background-color: rgba(76, 175, 80, 0.9);
}

.toast-error {
  background-color: rgba(244, 67, 54, 0.9);
}

.toast-warning {
  background-color: rgba(255, 193, 7, 0.9);
}

.toast-icon {
  margin-right: 8px;
}
</style>

更新全局服务

function showToast(message, options = {}) {
  const { type = 'info', duration = 2000 } = options
  if (!toastInstance) {
    toastInstance = new ToastConstructor({
      el: document.createElement('div')
    })
    document.body.appendChild(toastInstance.$el)
  }
  toastInstance.show(message, type, duration)
}

Vue.prototype.$toast = (message, options) => {
  if (typeof options === 'number') {
    showToast(message, { duration: options })
  } else {
    showToast(message, options)
  }
}

使用示例

this.$toast('操作成功', { type: 'success', duration: 3000 })
this.$toast('发生错误', 'error')
this.$toast('普通提示', 2000)

vue 实现toast组件

标签: 组件vue
分享给朋友:

相关文章

vue实现多选联动

vue实现多选联动

vue实现多选联动的方法 使用v-model绑定数组 在Vue中,可以通过v-model绑定一个数组来实现多选联动。当复选框被选中或取消选中时,数组会自动更新。 <template>…

vue实现密码框的包

vue实现密码框的包

Vue 密码框实现方案 在 Vue 中实现密码框功能,可以通过原生 HTML 结合 Vue 指令或使用第三方组件库完成。以下是几种常见方案: 原生 HTML + Vue 双向绑定 <temp…

vue请求实现进度条

vue请求实现进度条

实现请求进度条的方法 在Vue中实现请求进度条可以通过多种方式完成,常见的有使用第三方库如axios结合nprogress或自定义进度条组件。以下是几种实现方法: 使用axios和nprogres…

eventbus vue实现

eventbus vue实现

EventBus 实现原理 EventBus 是一种发布/订阅模式,用于 Vue 组件间的通信。核心是通过一个中央事件总线管理事件监听和触发。 创建 EventBus 在 Vue 项目中创建一个独立…

vue实现功能

vue实现功能

Vue 功能实现方法 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现常见功能的几种方法: 数据绑定与响应式更新 在 Vue 中,数据绑定通过 v-model…

vue 实现关闭

vue 实现关闭

Vue 实现关闭功能的方法 在Vue中实现关闭功能通常涉及组件销毁、模态框隐藏或路由跳转等场景。以下是几种常见实现方式: 销毁当前组件 使用v-if或v-show控制组件显示/隐藏,适用于局部组件:…