当前位置:首页 > VUE

vue实现toast

2026-02-10 06:15:05VUE

Vue 实现 Toast 的方法

使用第三方库(推荐)

安装 vue-toastification 或其他流行库:

npm install vue-toastification

main.js 中引入并配置:

import Toast from 'vue-toastification'
import 'vue-toastification/dist/index.css'

const options = {
  timeout: 3000,
  position: 'top-right'
}

Vue.use(Toast, options)

在组件中使用:

this.$toast.success('操作成功')
this.$toast.error('发生错误')
this.$toast.info('提示信息')

手动实现基础 Toast

创建 Toast.vue 组件:

vue实现toast

<template>
  <div v-if="show" class="toast" :class="type">
    {{ message }}
  </div>
</template>

<script>
export default {
  data() {
    return {
      show: false,
      message: '',
      type: 'info'
    }
  },
  methods: {
    display(message, type = 'info', duration = 3000) {
      this.message = message
      this.type = type
      this.show = true
      setTimeout(() => {
        this.show = false
      }, duration)
    }
  }
}
</script>

<style scoped>
.toast {
  position: fixed;
  top: 20px;
  right: 20px;
  padding: 12px 24px;
  border-radius: 4px;
  color: white;
  z-index: 9999;
}
.info { background-color: #2196F3; }
.success { background-color: #4CAF50; }
.error { background-color: #F44336; }
.warning { background-color: #FF9800; }
</style>

注册为全局组件:

// main.js
import Toast from '@/components/Toast'

const toast = {
  install(Vue) {
    const ToastConstructor = Vue.extend(Toast)
    const instance = new ToastConstructor()
    instance.$mount(document.createElement('div'))
    document.body.appendChild(instance.$el)

    Vue.prototype.$toast = {
      show(msg, type, duration) {
        instance.display(msg, type, duration)
      }
    }
  }
}

Vue.use(toast)

使用方式:

this.$toast.show('这是一个提示', 'success')

添加动画效果

在 Toast 组件中添加过渡效果:

vue实现toast

<template>
  <transition name="fade">
    <div v-if="show" class="toast" :class="type">
      {{ message }}
    </div>
  </transition>
</template>

<style scoped>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
</style>

支持队列显示

修改 Toast 组件的 methods:

methods: {
  display(message, type = 'info', duration = 3000) {
    this.queue.push({ message, type, duration })
    if (!this.isShowing) {
      this.showNext()
    }
  },
  showNext() {
    if (this.queue.length === 0) {
      this.isShowing = false
      return
    }

    this.isShowing = true
    const { message, type, duration } = this.queue.shift()
    this.message = message
    this.type = type
    this.show = true

    setTimeout(() => {
      this.show = false
      setTimeout(() => this.showNext(), 300)
    }, duration)
  }
}

可配置选项扩展

允许通过配置对象自定义 Toast:

this.$toast.show({
  message: '自定义内容',
  type: 'warning',
  duration: 5000,
  position: 'bottom-left'
})

在组件中处理位置样式:

<template>
  <div v-if="show" class="toast" :class="[type, position]">
    {{ message }}
  </div>
</template>

<style scoped>
.toast {
  /* 原有样式 */
}
.top-right { top: 20px; right: 20px }
.bottom-left { bottom: 20px; left: 20px }
/* 其他位置样式 */
</style>

标签: vuetoast
分享给朋友:

相关文章

vue实现按卡片轮播

vue实现按卡片轮播

实现卡片轮播的基本思路 在Vue中实现卡片轮播可以通过结合v-for指令和动态样式绑定完成。核心是维护一个当前显示卡片的索引,通过CSS过渡效果实现平滑切换。 基础实现步骤 模板部分 使用v-for…

vue里实现增删改查

vue里实现增删改查

在Vue中实现增删改查 数据初始化 在Vue组件的data中定义初始数据和表单绑定的变量: data() { return { items: [ { id: 1, name:…

vue实现博客

vue实现博客

Vue 实现博客的基本步骤 使用 Vue.js 实现一个博客系统可以分为前端和后端两部分。以下是基于 Vue 的前端实现方案,后端可以选择 Node.js、Django 或其他框架。 项目初始化 使…

vue diff实现

vue diff实现

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

vue实现webapp

vue实现webapp

Vue 实现 WebApp 的基本步骤 环境准备 确保已安装 Node.js 和 npm/yarn。通过 Vue CLI 或 Vite 创建项目: npm install -g @vue/cli v…

vue实现选择

vue实现选择

Vue 实现选择功能的方法 在 Vue 中实现选择功能可以通过多种方式完成,以下介绍几种常见的实现方法。 使用 v-model 绑定单选 通过 v-model 可以轻松实现单选功能。以下是一个简单的…