当前位置:首页 > VUE

实现vue message

2026-01-08 02:16:15VUE

实现 Vue 消息提示组件

创建 Message 组件src/components 目录下创建 Message.vue 文件,内容如下:

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

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

<style scoped>
.message {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  padding: 10px 20px;
  border-radius: 4px;
  color: white;
  z-index: 9999;
}

.info {
  background-color: #1890ff;
}

.success {
  background-color: #52c41a;
}

.warning {
  background-color: #faad14;
}

.error {
  background-color: #f5222d;
}

.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}

.fade-enter, .fade-leave-to {
  opacity: 0;
}
</style>

创建插件文件src/plugins 目录下创建 message.js 文件:

实现vue message

import Vue from 'vue'
import Message from '@/components/Message.vue'

const MessageConstructor = Vue.extend(Message)

const message = (options = {}) => {
  const instance = new MessageConstructor({
    data: options
  })
  instance.$mount()
  document.body.appendChild(instance.$el)
  instance.show(options.content, options.type, options.duration)
}

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

注册插件main.js 中注册插件:

import Vue from 'vue'
import Message from '@/plugins/message'

Vue.use(Message)

使用方式 在组件中调用:

实现vue message

this.$message({
  content: '这是一条提示消息',
  type: 'success',
  duration: 2000
})

// 或简写形式
this.$message.success('操作成功')
this.$message.error('操作失败')

扩展方法message.js 中添加快捷方法:

['success', 'warning', 'info', 'error'].forEach(type => {
  message[type] = (content, duration = 2000) => {
    return message({
      content,
      type,
      duration
    })
  }
})

全局挂载 如果需要全局挂载而非在每个组件中通过 this 调用:

// 在 message.js 中
const message = {
  install(Vue) {
    Vue.prototype.$message = messageFunction
  }
}

// 在 main.js 中
import message from '@/plugins/message'
Vue.use(message)
window.message = message

标签: vuemessage
分享给朋友:

相关文章

vue实现录音文件播放

vue实现录音文件播放

使用 Vue 实现录音文件播放 准备工作 确保项目中已安装必要的依赖,如 vue-audio-recorder 或原生 Web Audio API。录音文件播放通常涉及录音、存储和播放三个核心环节。…

vue实现选中单元格

vue实现选中单元格

Vue 实现选中单元格的方法 基础实现思路 在 Vue 中实现选中单元格功能,通常可以通过动态绑定 class 或 style 来实现。以下是一个基于表格的简单实现示例: <template…

vue实现同步

vue实现同步

Vue 实现同步的方法 在 Vue 中实现同步操作通常涉及数据绑定、状态管理和异步操作的处理。以下是几种常见的方法: 使用计算属性(Computed Properties) 计算属性基于响应式依赖…

vue实现switch

vue实现switch

Vue 实现 Switch 开关组件 在 Vue 中实现 Switch 开关组件可以通过自定义组件或使用现有 UI 库完成。以下是几种常见实现方式: 自定义 Switch 组件 创建一个基础…

vue实现定位

vue实现定位

Vue 中实现定位的方法 在 Vue 中实现定位可以通过以下几种方式: 使用 CSS 定位 通过 CSS 的 position 属性实现定位,Vue 中可以直接在组件的 <style>…

vue实现试卷

vue实现试卷

Vue实现试卷系统的基本思路 使用Vue.js构建试卷系统需要结合组件化开发、状态管理和后端数据交互。核心功能包括题目展示、答题逻辑、计时器和提交答案。 核心功能模块划分 试卷组件结构 试卷容器组…