2024-11-13 uniapp自定义全局弹窗(h5端)
效果:
代码目录:
新建uToast,里面建2个文件
index.js:
import fullNameVue from './index.vue' const FullToast = {}; FullToast.install = function (Vue, option) { const FullNameInstance = Vue.extend(fullNameVue); let name; const initInstance = () => { name = new FullNameInstance(); let nameSpan = name.$mount().$el; document.body.appendChild(nameSpan); } Vue.prototype.$uToast = { showToast(option) { initInstance(); if (typeof option === 'string') { name.firstName = option; } else if (typeof option === 'object') { Object.assign(name, option); } return initInstance; } }; } export default FullToast;
index.vue:
<template> <div class="fix-modal"> <div class="fix-modal-content"> <img class="fix-modal-content-img" :src="img" alt=""> <div class="fix-modal-content-action"> <div class="fix-modal-content-action-title">如需帮助,请联系我们</div> <div class="fix-modal-content-action-btn">点击联系平台客服</div> </div> </div> </div> </template> <script> export default { name: 'dialog', data() { return { visible: true, img: '../static/modal.png' }; }, }; </script> <style lang='scss' scoped> .fix-modal { position: fixed; top: 0; left: 0; right: 0; bottom: 0; margin: auto; width: 100vw; height: 100vh; background-color: rgba(0, 0, 0, 0.5); z-index: 99999999999999; overflow-y: hidden; display: flex; align-items: center; justify-content: center; .fix-modal-content { width: 80vw; height: auto; background-color: #fff; border-radius: 4px; padding: 0 20px; .fix-modal-content-img { width: 100%; } .fix-modal-content-action { border: 1px solid #d5e2f5; border-radius: 20px; display: flex; align-items: center; justify-content: center; padding: 20px; flex-direction: column; gap: 20px; position: relative; top: -4px; .fix-modal-content-action-title {} .fix-modal-content-action-btn { background-color: #0067fd; color: #fff; border-radius: 50px; width: 100%; padding: 20px; display: flex; align-items: center; justify-content: center; flex-direction: column; } margin-bottom: 16px; } } } </style>
在main.js中引入并使用:
import uToast from '@/components/uToast/index'
Vue.use(uToast);
在vue中引用:
this.$uToast.showToast();
在js中使用:
import Vue from 'vue' const vm = new Vue() vm.$uToast.showToast()
分类:
uniapp
, Vue.js 2.0
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
2023-11-13 2023-11-13 electron-egg(学习笔记)