当前位置: 首页 > news >正文

vue 实现自定义message 全局提示

message组件

<template><transition name="fade"><div v-if="visible" class="m-message" :class="`m-message-${type}`"><svg v-if="type == 'success'" fill="none" viewBox="0 0 24 24" width="1em" height="1em" class="t-icon t-icon-error-circle-filled" style="fill: none"><pathfill="currentColor"d="M12 1C18.0751 1 23 5.92487 23 12C23 18.0751 18.0751 23 12 23C5.92487 23 1 18.0751 1 12C1 5.92487 5.92487 1 12 1ZM11.0001 14H13.0001V6.49998H11.0001V14ZM13.004 15.5H11.0001V17.5039H13.004V15.5Z"></path></svg><svgv-else-if="type == 'error'"fill="none"viewBox="0 0 24 24"width="1em"height="1em"class="t-icon t-icon-info-circle-filled"style="fill: none"><pathfill="currentColor"d="M12 23C18.0751 23 23 18.0751 23 12C23 5.92487 18.0751 1 12 1C5.92487 1 1 5.92487 1 12C1 18.0751 5.92487 23 12 23ZM10.996 8.50002V6.49611H12.9999V8.50002H10.996ZM12.9999 10L12.9999 17.5H10.9999V10L12.9999 10Z"></path></svg><svgv-else-if="type == 'warning'"fill="none"viewBox="0 0 24 24"width="1em"height="1em"class="t-icon t-icon-error-circle-filled"style="fill: none"><pathfill="currentColor"d="M12 1C18.0751 1 23 5.92487 23 12C23 18.0751 18.0751 23 12 23C5.92487 23 1 18.0751 1 12C1 5.92487 5.92487 1 12 1ZM11.0001 14H13.0001V6.49998H11.0001V14ZM13.004 15.5H11.0001V17.5039H13.004V15.5Z"></path></svg><svgv-else-if="type == 'info'"fill="none"viewBox="0 0 24 24"width="1em"height="1em"class="t-icon t-icon-info-circle-filled"style="fill: none"><pathfill="currentColor"d="M12 23C18.0751 23 23 18.0751 23 12C23 5.92487 18.0751 1 12 1C5.92487 1 1 5.92487 1 12C1 18.0751 5.92487 23 12 23ZM10.996 8.50002V6.49611H12.9999V8.50002H10.996ZM12.9999 10L12.9999 17.5H10.9999V10L12.9999 10Z"></path></svg>{{ text }}</div></transition>
</template><script setup lang="ts">
import { ref, onMounted } from "vue";const props = defineProps({text: String,type: {type: String,default: "info", // success | error | warning | info},duration: {type: Number,default: 3000,},
});const visible = ref(false);onMounted(() => {visible.value = true;setTimeout(() => {visible.value = false;}, props.duration);
});
</script><style lang="scss" scoped>
@media screen and (min-width: 768px) {.m-message {display: flex;align-items: center;gap: 8px;padding: 12px 16px;width: fit-content;height: fit-content;background-color: #fff;border-radius: 6px;color: #041e39;font-size: 14px;box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);opacity: 0.95;white-space: nowrap;& > svg {width: 16px;height: 16px;}}
}@media screen and (max-width: 768px) {.m-message {display: flex;align-items: center;gap: 0.8rem;padding: 1.2rem 1.6rem;width: fit-content;height: fit-content;background-color: #fff;border-radius: 0.6rem;color: #041e39;font-size: 1.4rem;box-shadow: 0 0.2rem 0.8rem rgba(0, 0, 0, 0.15);opacity: 0.95;white-space: nowrap;& > svg {width: 1.6rem;height: 1.6rem;}}
}/* 不同类型的颜色 */
.m-message-success {color: #67c23a;
}
.m-message-error {color: #f56c6c;
}
.m-message-warning {color: #e6a23c;
}
.m-message-info {color: #909399;
}/* 动画 */
.fade-enter-active,
.fade-leave-active {transition: all 0.3s ease;
}
.fade-enter-from,
.fade-leave-to {opacity: 0;transform: translateY(-10px);
}
</style>

        注册成方法

import { createVNode, render } from "vue";
import MessageComponent from "@/components/M/Message/index.vue";type MessageType = "success" | "error" | "warning" | "info";// 维护当前已显示的消息节点容器
const messageInstances: HTMLElement[] = [];function showMessage(type: MessageType, text: string, duration = 2000) {const container = document.createElement("div");container.className = "custom-message-container";document.body.appendChild(container);const vnode = createVNode(MessageComponent, { text, type, duration });render(vnode, container);// 记录实例messageInstances.push(container);// 调整每个消息的位置(根据索引累积 20px)updateMessagePositions();// 定时销毁节点setTimeout(() => {render(null, container);document.body.removeChild(container);messageInstances.splice(messageInstances.indexOf(container), 1);updateMessagePositions();}, duration + 500);
}// 更新所有 message 的位置
function updateMessagePositions() {messageInstances.forEach((container, index) => {const offset = index == 0 ? 12 : index * 62; // 每条间隔12pxcontainer.style.position = "fixed";container.style.top = `${offset}px`;container.style.left = "50%";container.style.width = "fit-content";container.style.transform = "translateX(-50%)";container.style.zIndex = "9999";container.style.transition = "all 0.3s";});
}export const Message = {success: (text: string, duration?: number) => showMessage("success", text, duration),error: (text: string, duration?: number) => showMessage("error", text, duration),warning: (text: string, duration?: number) => showMessage("warning", text, duration),info: (text: string, duration?: number) => showMessage("info", text, duration),
};export default Message;

使用:

import message from "@/utils/message.ts";message.error("6666");

http://www.dtcms.com/a/564867.html

相关文章:

  • 电商网站里的图片网站开发中 视频播放卡
  • [手机AI开发sdk] 模型冻结解冻.pb | `aidlite`加速AI模型
  • 2025 年热门 CV 会议论文【源码复现】:Neural Inverse Rendering from Propagating Light
  • 中小企业网站建设与管理南通网站排名团队
  • TypeScript 队列实战:从零实现简单、循环、双端、优先队列,附完整测试代码
  • LeetCode hot100:189 轮转数组:三种解法从入门到精通
  • 初识MYSQL —— 基本查询
  • 项目打包与部署 —— 把 Java 项目 “装瓶带走”(本地运行→服务器落地全流程)
  • 湘潭做网站价格找磐石网络一流河北网站建设哪家好
  • React 11 登录页项目框架搭建
  • MySQL物理备份之Percona XtraBackup
  • 糖尿病预测多个机器学习维度预测
  • CSP-J教程——第一阶段——第三课:基本的输入与输出
  • 营销网站怎么做合适全站搜索
  • 解决IntelliJ IDEA控制台输出中文乱码问题
  • 昆仑芯 X HAMi X 百度智能云 | 昆仑芯 P800 XPU/vXPU 双模式算力调度方案落地
  • HarmonyOS6.0开发实战:HTTP 网络请求与 API 交互全指南
  • 合肥网站开发建设wordpress使用难不难
  • 杭州市上城区建设局网站江阴网页设计
  • 【软考】信息系统项目管理师-进度管理论文范文
  • 开关电源的短路保护如何测试?又需要哪些仪器呢?-纳米软件
  • 从 0 到 1 掌握医学图像分割 的完整实战指南
  • HTML应用指南:利用POST请求获取全国爱回收门店位置信息
  • 在线下载免费软件的网站网页设计模板图片html
  • 第九天 - psutil系统监控库 - 资源监控仪表盘 - 练习:实时CPU/Memory监控
  • CentOS/AlmaLinux 9 中 SSH 服务启动失败:OpenSSL 版本不匹配解决
  • MAC-SQL 论文翻译
  • 海宁最火高端网站设计推荐crack wordpress
  • Kanass零基础学习,如何进行任务管理
  • 3 个诊断 Linux 服务器的脚本