当前位置:首页 > 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 等不同类型。

vue 实现toast组件

<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
分享给朋友:

相关文章

vue 实现豆瓣

vue 实现豆瓣

以下是基于 Vue 实现豆瓣电影类功能的实现方案,涵盖核心模块和技术要点: 数据获取与 API 调用 使用豆瓣开放 API(需注意调用频率限制)或第三方代理接口 推荐 axios 进行异步请求,配合…

vue实现图集

vue实现图集

Vue 实现图集的方法 在 Vue 中实现图集功能可以通过多种方式完成,以下是一些常见的方法和实现步骤。 使用第三方库(如 vue-image-lightbox) 安装 vue-image-ligh…

vue实现过程

vue实现过程

Vue 的实现过程 Vue 的实现过程可以分为多个核心模块,包括响应式系统、虚拟 DOM、模板编译等。以下是 Vue 的主要实现过程分析。 响应式系统 Vue 的响应式系统基于 Object.def…

vue实现rtc

vue实现rtc

Vue 实现 RTC 的步骤 安装依赖 在 Vue 项目中安装 WebRTC 相关依赖,如 simple-peer 或 peerjs,用于简化 WebRTC 的实现。 运行以下命令安装 simple-…

vue 实现注册

vue 实现注册

Vue 实现注册功能 注册功能通常包括表单验证、数据提交和用户反馈。以下是实现注册功能的步骤: 1. 创建注册表单组件 在 Vue 项目中创建一个注册表单组件,包含用户名、邮箱、密码等输入字段。…

vue diff实现

vue diff实现

Vue Diff 算法实现原理 Vue 的虚拟 DOM Diff 算法(即 patch 过程)是其高效渲染的核心,主要通过对比新旧虚拟节点的差异,最小化 DOM 操作。以下是关键实现要点: 同层级比…