Vue实现ElementUI中Message组件

Vue实现Element-ui中Message组件


首先新建一个plugin文件夹

message.js

import Vue from 'vue'
import messageComponent from './message.vue'
let vm;
const getInstance = () => {
  //单例模式  防止生成多个dom
  if (!vm) {
    vm = new Vue({
      render: h => h(messageComponent)
    }).$mount()  //render =》 dom  把render转化为dom元素  只生成  不挂载 放在内存中
    //$mount('#app')  把生成的元素挂载到#app上
    document.body.appendChild(vm.$el) //$el  保存实例生成的dom元素
  }
  return vm.$children[0]
}
//message.info
const message = {
  info(options) {
    getInstance().add(options)
  }
}
export {
  message
}

message.vue

<template>
  <div class="el-message">
    <div v-for="m in message" :key='m.id'>{{m.message}}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      message:[],
      id:0
    }
  },
  mounted(){
    this.id=0
  },
  methods: {
    add(options){
      const id=this.id++
      const layer={...options,id}
      this.message.push(layer)
      setTimeout(()=>{
        this.remove(layer)
      },options.duration)
    },
    remove(layer){
      this.message=this.message.filter(message=>{
        return message.id!==layer.id
      })
    }
  },
}
</script>

<style>

</style>

组件中引入

<template>
  <div class="home">
    <button @click="pushMessage">添加</button>
  </div>
</template>

<script>
import {message} from '../plugin/message'
export default {
  name: 'Home',
  methods: {
    pushMessage(){
      message.info({
        message:'这是一条消息',
        duration:2000
      })
    }
  },
  
}
</script>

效果
在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
实现消息通知,可以借助VueElementUI和Websockets,以下是基本的实现步骤: 1. 安装VueElementUI 使用命令行工具,进入项目目录,执行以下命令: ``` npm install vue npm install element-ui ``` 2. 引入ElementUI组件Vue的入口文件,引入需要使用的ElementUI组件,例如Notification、MessageBox等。 ``` import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI) ``` 3. 创建WebSocket连接 在Vue组件通过Websockets连接后端服务器,建立WebSocket连接,接收服务器推送的消息。 ``` // 建立WebSocket连接 const webSocket = new WebSocket('ws://localhost:8080') // 监听WebSocket连接打开事件 webSocket.onopen = function () { console.log('WebSocket连接已打开') } // 监听WebSocket接收到消息事件 webSocket.onmessage = function (event) { console.log('WebSocket接收到消息:' + event.data) // 处理接收到的消息 } // 监听WebSocket连接关闭事件 webSocket.onclose = function () { console.log('WebSocket连接已关闭') } ``` 4. 处理接收到的消息 在WebSocket监听到有消息时,可以使用ElementUI的Notification组件弹出通知框,提醒用户有新消息。 ``` // 处理WebSocket接收到的消息 webSocket.onmessage = function (event) { console.log('WebSocket接收到消息:' + event.data) // 弹出通知框 const msg = JSON.parse(event.data) Notification({ title: '新消息', message: msg.content }) // 弹出确认框 MessageBox.confirm(msg.content, '新消息', { confirmButtonText: '查看详情', cancelButtonText: '忽略', type: 'info' }).then(() => { // 用户点击了查看详情按钮 // 处理跳转逻辑 }).catch(() => { // 用户点击了忽略按钮 // 不做任何处理 }) } ``` 以上就是使用VueElementUI和Websockets实现消息通知的基本步骤。需要注意的是,WebSocket连接的建立和关闭需要在组件的mounted和beforeDestroy生命周期函数分别进行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值