当前位置:首页 > VUE

实现vue message

2026-01-13 07:12:19VUE

Vue 消息提示组件实现

基本组件结构

创建一个独立的Message.vue组件,包含模板、样式和逻辑部分。模板部分定义消息框的HTML结构,样式部分控制消息框的外观和动画效果,逻辑部分处理消息的显示和隐藏。

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

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

<style scoped>
.message {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  padding: 10px 20px;
  background-color: #f0f9eb;
  color: #67c23a;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  z-index: 9999;
  animation: fadein 0.3s;
}

@keyframes fadein {
  from { opacity: 0; top: 0; }
  to { opacity: 1; top: 20px; }
}
</style>

全局注册与调用

在Vue项目中创建一个插件文件message.js,用于全局注册消息组件并提供调用方法。通过Vue.extend创建组件构造器,动态挂载到DOM中。

import Vue from 'vue'
import MessageComponent from './Message.vue'

const MessageConstructor = Vue.extend(MessageComponent)

const message = function(options) {
  const instance = new MessageConstructor({
    data: typeof options === 'string' ? { content: options } : options
  })

  instance.$mount()
  document.body.appendChild(instance.$el)
  instance.show()

  return instance
}

const types = ['success', 'warning', 'error', 'info']
types.forEach(type => {
  message[type] = options => {
    return message({
      ...(typeof options === 'string' ? { content: options } : options),
      type
    })
  }
})

export default message

样式扩展

根据消息类型添加不同的样式类,增强视觉反馈。在Message.vue的样式中增加不同类型消息的样式定义。

.message.success {
  background-color: #f0f9eb;
  color: #67c23a;
}

.message.warning {
  background-color: #fdf6ec;
  color: #e6a23c;
}

.message.error {
  background-color: #fef0f0;
  color: #f56c6c;
}

.message.info {
  background-color: #f4f4f5;
  color: #909399;
}

动画优化

添加进场和退场动画效果,提升用户体验。修改Message.vue的样式部分,增加退场动画定义。

.message.leaving {
  animation: fadeout 0.3s;
}

@keyframes fadeout {
  from { opacity: 1; top: 20px; }
  to { opacity: 0; top: 0; }
}

组件方法更新

修改Message.vue的show方法,在隐藏时添加退场动画类并延迟移除DOM元素。

show() {
  this.visible = true
  setTimeout(() => {
    this.$el.classList.add('leaving')
    setTimeout(() => {
      this.$el.remove()
    }, 300)
  }, this.duration)
}

多实例管理

在message.js中添加实例管理逻辑,防止多次调用产生重叠问题。维护一个实例数组,自动计算位置偏移。

实现vue message

let instances = []
let seed = 1

const message = function(options) {
  const id = 'message_' + seed++
  const instance = new MessageConstructor({
    data: {
      ...(typeof options === 'string' ? { content: options } : options),
      id
    }
  })

  let verticalOffset = 20
  instances.forEach(item => {
    verticalOffset += item.$el.offsetHeight + 16
  })
  instance.verticalOffset = verticalOffset

  instance.$mount()
  document.body.appendChild(instance.$el)
  instance.visible = true

  instances.push(instance)
  return instance
}

标签: vuemessage
分享给朋友:

相关文章

vue手动实现弹窗

vue手动实现弹窗

实现弹窗组件的基本结构 在Vue中手动实现弹窗组件,需要创建一个独立的弹窗组件,并通过props控制其显示与隐藏。弹窗组件通常包含遮罩层、内容区域和关闭按钮。 <template>…

vue实现iframe

vue实现iframe

Vue 中实现 iframe 的方法 在 Vue 项目中,可以通过多种方式实现 iframe 的嵌入。以下是几种常见的方法: 使用原生 HTML iframe 标签 直接在 Vue 组件的模板中使用…

vue实现键盘

vue实现键盘

Vue 实现键盘功能 在Vue中实现键盘功能通常涉及监听键盘事件、处理输入以及渲染虚拟键盘界面。以下是几种常见的实现方式: 监听键盘事件 通过@keydown或@keyup指令监听键盘事件,适用于需…

vue实现toggle

vue实现toggle

Vue 实现 Toggle 功能 在 Vue 中实现 toggle(切换)功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 和 v-on 通过 v-model 绑定数据,结合…

vue实现导出

vue实现导出

Vue 实现导出功能的方法 在 Vue 项目中实现导出功能通常涉及将数据导出为 Excel、CSV 或其他格式。以下是几种常见的实现方式: 使用 xlsx 库导出 Excel 安装 xlsx 库:…

vue 实现轮播

vue 实现轮播

Vue 实现轮播的方法 使用第三方库(如 Swiper) 安装 Swiper 库: npm install swiper 在 Vue 组件中引入 Swiper: <template>…