Vue消息提示this.$message方法使用解读

 更新时间:2023年09月26日 08:36:22   作者:老李四  
这篇文章主要介绍了Vue消息提示this.$message方法使用,具有很好的参考价值,希望对大家有所帮助,

Vue消息提示this.$message方法

HTML

<el-button @click="saveData">弹窗</el-button>

JavaScript

saveData(){
    this.$message({undefined
        message:“这是弹框消息”,
        type:‘success'
     })
 // type 取值 ‘success'(成功) /warning(警告)/info(消息)/error(错误)/
}

element 的 this.$message( ) 消息提示实现

在vue项目中,直接通过js代码 this.$message( )就可以调出消息提示组件,这是如何实现的呢

主要分为以下几步

1.用 Vue.extend 创建组件的模板(构造函数)

2.创建一个函数,在函数内部, 实例化组件并进行挂载到相应元素上

3.将创建的这个函数绑定到Vue原型上,就可以通过this .访问了

上代码,

如下目录结构

在main.js中

import Vue from "vue";
import message from "./main.vue";
// 1.用 Vue.extend 创建组件的模板(构造函数)
let messageConstructor = Vue.extend(message);
let instance;
const Message = function(options) {// options是传入的参数配置 {message: '成功',type: "success"offset: 80}
  // 2.实例化组件
  instance = new messageConstructor({ data: options }); // 把options导入data中
  // 3.组件挂载
  instance.$mount();
  document.body.appendChild(instance.$el);
  // 设置offset
  let verticalOffset = options.offset || 20;
  instance.verticalOffset = verticalOffset;
  instance.duration = options.duration || 3000;
  return instance;
};
export default Message;

在main.vue中

 
<template>
  <div  v-show="visible"
    :class="['my-message', 'my-message-' + type, center ? 'is-center' : '']"
    :style="positionStyle"
  >
    <slot>
      <p>{{ message }}</p>
    </slot>
    <i v-if="showClose" class="el-icon-close" @click="close"></i>
  </div>
</template>
<script>
export default {
  name: "Message",
  data() {
    return {
      visible:false,
      verticalOffset: 20,
      duration: 3000,
      timer: null,
      center: false,
      showClose: false,
       closed: false,
    };
  },
  mounted() {
    this.autoClose();
  },
  beforeDestroy() {
    clearTimeout(this.timer);
  },
    watch: {
      closed(newVal) {
        if (newVal) {
          this.visible = false;
        }
      }
    },
  computed: {
    positionStyle() {
      return {
        top: `${this.verticalOffset}px`,
      };
    },
  },
  methods: {
    autoClose() {
      this.timer = setTimeout(() => {
        this.$destroy(true);
        this.$el.parentNode.removeChild(this.$el);
      }, this.duration);
    },
    close() {
      this.closed = true;
      clearTimeout(this.timer);
    }
  },
};
</script>
<style>
.my-message {
  min-width: 380px;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  position: fixed;
  left: 50%;
  top: 20px;
  transform: translateX(-50%);
  background-color: #edf2fc;
  transition: opacity 0.3s, transform 0.4s, top 0.4s;
  overflow: hidden;
  display: flex;
  align-items: center;
  padding: 0 15px;
}
p {
}
.my-message-info {
  color: #909399;
}
.my-message-success {
  background: #f2f9ec;
  color: #67c23a;
  border-color: #e4f2da;
}
.my-message-warning {
  background: #fcf6ed;
  color: #e6a23c;
  border-color: #f8ecda;
}
.my-message-error {
  background: #fcf0f0;
  color: #f56c6c;
  border-color: #f9e3e2;
}
.is-center {
  justify-content: center;
}
</style>

在index.js中

import Vue from "vue";
import Message from './src/main'
Vue.prototype.$message = Message 

ok了,大晚上的有点困,有些地方有些潦草,有时间再改下。。。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Vue Router参数传递的多种方式小结

    Vue Router参数传递的多种方式小结

    在 Vue.js 开发中,vue-router 是构建单页面应用(SPA)的核心工具之一,它不仅能帮助我们管理路由,还能在不同页面之间传递参数,本文将详细介绍 vue-router 中常见的参数传递方式,并通过实际示例帮助你快速掌握这些技巧,需要的朋友可以参考下
    2025-04-04
  • VueJS 取得 URL 参数值的方法

    VueJS 取得 URL 参数值的方法

    form-create 是一个可以通过 JSON 生成具有动态渲染、数据收集、验证和提交功能的表单生成器。本文给大家简单介绍了VueJS U取得RL 参数值的方法,详细给大家介绍了vue自定义表单生成器可根据json参数动态生成表单效果,感兴趣的朋友一起看看吧
    2019-07-07
  • Vue项目中keepAlive的使用说明(超级实用版)

    Vue项目中keepAlive的使用说明(超级实用版)

    这篇文章主要介绍了Vue项目中keepAlive的使用说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04
  • uniapp微信小程序webview和h5数据通信代码示例

    uniapp微信小程序webview和h5数据通信代码示例

    这篇文章主要介绍了uniapp微信小程序webview和h5数据通信的相关资料,文章还列出了项目的结构,包括微信小程序和h5端的主要文件和组件,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2025-01-01
  • django中使用vue.js的要点总结

    django中使用vue.js的要点总结

    在本篇文章里小编给各位整理了关于django中使用vue.js需要注意的地方以及相关知识点,需要的朋友们跟着学习参考下。
    2019-07-07
  • Vue报错Module build failed: Error: Node Sass version 7.0.1 is incompatible with 4.0.0.解决方案

    Vue报错Module build failed: Error: Node&nb

    这篇文章主要介绍了Vue报错Module build failed: Error: Node Sass version 7.0.1 is incompatible with 4.0.0.解决方案,需要的朋友可以参考下
    2023-06-06
  • 详解Vue 中 extend 、component 、mixins 、extends 的区别

    详解Vue 中 extend 、component 、mixins 、extends 的区别

    这篇文章主要介绍了Vue 中 extend 、component 、mixins 、extends 的区别,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-12-12
  • Vue上传组件vue Simple Uploader的用法示例

    Vue上传组件vue Simple Uploader的用法示例

    本篇文章主要介绍了Vue上传组件vue Simple Uploader的用法示例,非常具有实用价值,需要的朋友可以参考下
    2017-08-08
  • Vue2中使用自定义指令实现el-table虚拟列表的代码示例

    Vue2中使用自定义指令实现el-table虚拟列表的代码示例

    在实际开发中,我们可能会面临其他需求,例如在 el-table 中无法使用分页技术的情况下展示海量数据,这种情况下,页面可能会出现卡顿,严重时甚至可能引发浏览器崩溃,所以针对这个问题本文给大家介绍了vue2中使用自定义指令实现el-table虚拟列表,需要的朋友可以参考下
    2025-01-01
  • 基于vue框架手写一个notify插件实现通知功能的方法

    基于vue框架手写一个notify插件实现通知功能的方法

    这篇文章主要介绍了基于vue框架手写一个notify插件实现通知功能的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03

最新评论