当前位置:首页 > VUE

vue实现 toast

2026-01-12 21:05:10VUE

Vue 实现 Toast 的方法

使用第三方库(推荐)

安装 vue-toastification 库,这是一个功能丰富且易于使用的 Vue Toast 插件。

npm install vue-toastification

在 Vue 项目中全局注册插件:

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

const app = createApp(App)
app.use(Toast)

在组件中使用 Toast:

this.$toast.success('操作成功')
this.$toast.error('操作失败')
this.$toast.info('提示信息')
this.$toast.warning('警告信息')

自定义 Toast 组件

创建一个可复用的 Toast 组件,适合简单需求。

Toast.vue

vue实现 toast

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

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

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

在组件中使用

import Toast from '@/components/Toast.vue'

export default {
  components: { Toast },
  methods: {
    showToast() {
      this.$refs.toast.showToast('自定义Toast消息')
    }
  }
}

使用 Vue 的 provide/inject

通过 provide/inject 实现全局 Toast 功能。

main.js

vue实现 toast

import { createApp } from 'vue'
import App from './App.vue'

const app = createApp(App)

app.provide('toast', {
  show(message, duration = 2000) {
    const toast = document.createElement('div')
    toast.className = 'toast'
    toast.textContent = message
    document.body.appendChild(toast)
    setTimeout(() => {
      document.body.removeChild(toast)
    }, duration)
  }
})

app.mount('#app')

在组件中使用

export default {
  inject: ['toast'],
  methods: {
    showToast() {
      this.toast.show('通过provide/inject实现的Toast')
    }
  }
}

使用 Vue 3 Composition API

在 Vue 3 中可以使用 Composition API 创建更灵活的 Toast。

useToast.js

import { ref } from 'vue'

export default function useToast() {
  const toast = ref({
    show: false,
    message: ''
  })

  const showToast = (message, duration = 2000) => {
    toast.value.show = true
    toast.value.message = message
    setTimeout(() => {
      toast.value.show = false
    }, duration)
  }

  return { toast, showToast }
}

在组件中使用

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

<script>
import useToast from '@/composables/useToast'

export default {
  setup() {
    const { toast, showToast } = useToast()

    return { toast, showToast }
  }
}
</script>

以上方法提供了不同复杂度的 Toast 实现方案,可以根据项目需求选择合适的方案。第三方库方案功能最完善,自定义组件方案灵活性最高。

标签: vuetoast
分享给朋友:

相关文章

vue实现继承语法糖

vue实现继承语法糖

在Vue中实现继承可以通过多种方式模拟类继承的效果,以下是常见的几种方法: 使用mixins实现逻辑复用 通过Vue的mixins机制共享组件选项,达到类似继承的效果: const BaseC…

vue实现闪烁

vue实现闪烁

Vue实现元素闪烁效果 使用CSS动画实现 通过Vue绑定class结合CSS动画实现闪烁效果,代码简洁且性能较好。 <template> <div :class="{ 'b…

vue树形实现

vue树形实现

Vue 树形组件实现方法 使用递归组件实现树形结构 递归组件是 Vue 中实现树形结构的常见方法。通过组件调用自身的方式,可以轻松构建多层嵌套的树形结构。 <template> &l…

vue 实现打印

vue 实现打印

Vue 实现打印功能的方法 在Vue项目中实现打印功能,可以通过以下几种方式实现: 使用window.print()方法 通过调用浏览器的原生打印API实现基础打印功能,适用于简单内容打印。 //…

vue实现fullpage

vue实现fullpage

Vue 实现 FullPage 效果 在 Vue 中实现全屏滚动(FullPage)效果可以通过多种方式完成,以下是几种常见的方法: 使用现有库 vue-fullpage.js 安装 vue-fu…

vue实现webapp

vue实现webapp

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