如何写一个全局的 Notice 组件?
下面将会实现这样的效果:
组件动态创建脚本:
import Vue from "vue";
import Notice from "@/components/Noticer/Notice.vue"; function create(Component, props) {
// 先建立实例
const vm = new Vue({
render(h) {
//h就是createElement,它返回VNode
return h(Component, { props });
},
}).$mount();
// 手动挂载 // 判断是否存在container,如果不存在则先创建
let container;
container = document.querySelector(".noticer-container");
if (container == null) {
container = document.createElement("div");
container.classList.add("noticer-container");
container.style.position = "fixed";
container.style.top = "50px";
container.style.right = "0px";
container.style.overflow = "hidden";
container.style.zIndex = 9999; document.body.appendChild(container);
} container.appendChild(vm.$el); //销毁方法
const comp = vm.$children[0];
comp.remove = function () {
container.removeChild(comp["$el"]);
vm.$destroy();
};
comp.show();
return comp;
} Vue.prototype.$notice = {
error: function (props) {
create(Notice, Object.assign(props, { type: "error" }));
},
info: function (props) {
create(Notice, Object.assign(props, { type: "info" }));
},
success: function (props) {
create(Notice, Object.assign(props, { type: "success" }));
},
warn: function (props) {
create(Notice, Object.assign(props, { type: "warn" }));
},
};这里有一些值得注意的地方:
- container: 的作用是notice的容器,它可以用于定位notice在页面的哪里展示,注意notice不应该随页面卷动,因此其定位是
fixed
, 而之所以设定为overflow:hidden
的原因则是,notice 在出现和移除的时候,发生的动画偏移,会让页面出现横向滚动条。为了避免重复创建container, 这里做了一个判断逻辑。然后所有动态生成的notice实例dom都会通过appendChild
添加到这个容器。 - 在移除的时候, 移除的是
vm.$children[0]["$el"]
, 原因是,Notice 模板的实现中,外层套了一个 transition , 而这个transition是并不会渲染dom的。
- container: 的作用是notice的容器,它可以用于定位notice在页面的哪里展示,注意notice不应该随页面卷动,因此其定位是
创建Notice组件模板:
<template>
<transition
enter-active-class="animate__animated animate__slideInRight"
leave-active-class="animate__animated animate__slideOutRight"
@after-leave="afterLeave"
>
<div v-if="isShow" class="notice__root">
<div :class="`notice-type-${type}`" class="noticer">
{{
type === "error"
? "🍓"
: type === "success"
? "🍀"
: type === "warn"
? "🍋"
: "🐳"
}}
: {{ message }}
</div>
</div>
</transition>
</template>
<script>
export default {
props: {
title: {
type: String,
default: "",
},
message: {
type: String,
default: "",
},
time: {
type: Number,
default: 1000,
},
type: {
type: String,
},
},
data() {
return {
isShow: false,
};
},
methods: {
show() {
this.isShow = true;
setTimeout(this.hide, this.time);
},
hide() {
this.isShow = false;
},
afterLeave() {
this.remove();
},
},
};
</script>
<style lang="less" scoped>
@error: rgb(255, 30, 30);
@warn: rgb(240, 192, 0);
@success: rgb(0, 144, 74);
@info: rgb(0, 80, 218); @errorBg: rgb(255, 208, 208);
@warnBg: rgb(255, 245, 207);
@successBg: rgb(210, 255, 233);
@infoBg: rgb(203, 222, 255); .notice__root {
user-select: none;
padding: 5px 50px 5px 5px;
} .noticer {
padding: 5px 20px;
margin: 10px 0px;
// margin-right: 50px;
border-radius: 8px;
font-size: 16px;
width: auto;
min-width: 280px;
max-width: 300px;
word-break: break-all;
text-align: center;
box-sizing: border-box;
}
.notice-type-error {
color: @error !important;
border: 2px solid @error;
box-shadow: 1px 1px 5px 2px @errorBg;
background-color: @errorBg; // border: 1px solid red;
}
.notice-type-warn {
color: @warn !important;
border: 2px solid @warn;
background-color: @warnBg;
box-shadow: 1px 1px 5px 2px @warnBg;
}
.notice-type-success {
color: @success !important;
border: 2px solid @success;
background-color: @successBg;
box-shadow: 1px 1px 5px 2px @successBg;
}
.notice-type-info {
color: @info !important;
border: 2px solid @info;
background-color: @infoBg;
box-shadow: 1px 1px 5px 2px @infoBg;
}
</style>
在 main.js 中引入执行该脚本即可
import Vue from "vue";
import App from "./App.vue";
import "animate.css";
import "@/components/Noticer/NotificationBanner.js"; new Vue({
render: (h) => h(App),
}).$mount("#app");
代码中使用实例:
if (!this.nickname) {
this.$notice.error({
message: "好汉!姓甚名谁?",
time: 3000,
});
} else {
this.showModal = false;
this.$notice.info({
message: this.nickname + "来了!!!",
time: 3000,
});
}
动态创建组件的执行逻辑:
当在使用的时候:
this.$notice.error({
message: "好汉!姓甚名谁?",
time: 3000,
});
上方代码触发,实际上会触发 NotificationBanner.js 中的 create
函数,该函数创建了一个notice 的组件实例,并在实力上添加了一个remove
方法,然后直接触发组件中的 show
方法。
notice 模板实例中:
methods: {
show() {
this.isShow = true;
setTimeout(this.hide, this.time);
},
hide() {
this.isShow = false;
},
afterLeave() {
this.remove();
},
},
show
方法执行,除了展示 notice,立即设定一个延时函数执行 hide
方法。
hide
方法执行, vue 提供的 transition 钩子 afterleave()
会在移除动画执行完毕后触发。 这时候,去触发 remove
方法,将该notice 组件实例移除。
如何写一个全局的 Notice 组件?的更多相关文章
- 手写一个简单的starter组件
spring-boot中有很多第三方包,都封装成starter组件,在maven中引用后,启动springBoot项目时会自动装配到spring ioc容器中. 思考: 为什么我们springBoot ...
- Tsx写一个通用的button组件
一年又要到年底了,vue3.0都已经出来了,我们也不能一直还停留在过去的js中,是时候学习并且在项目中使用一下Ts了. 如果说jsx是基于js的话,那么tsx就是基于typescript的 废话也不多 ...
- python安装及写一个简单的验证码组件(配合node)
1.安装Python 到官网下载响应系统的版本(这里以windows为例):https://www.python.org/downloads/windows/ 然后就是不断地"下一步&quo ...
- Vue学习—Vue写一个图片轮播组件
1.先看效果: 熟悉的图片轮播,只要是个网站,百分之90以上会有个图片轮播.我认为使用图片轮播. 第一可以给人以一种美观的感受,而不会显得网站那么呆板, 第二可以增加显示内容,同样的区域可以显示更多内 ...
- 用vue2.x注册一个全局的弹窗alert组件
一.在实际的开发当中,弹窗是少不了的,默认系统的弹窗样式太丑,难以满足项目的实际需求,所以需要自己定义弹窗组件,把弹窗组价定义为全局的,这样减少每次使用的时候引入麻烦,节省开发时间.本文将分享如何定义 ...
- 用vue2.x注册一个全局的弹窗alert组件、confirm组件
一.在实际的开发当中,弹窗是少不了的,默认系统的弹窗样式太丑,难以满足项目的实际需求,所以需要自己定义弹窗组件,把弹窗组价定义为全局的,这样减少每次使用的时候引入麻烦,节省开发时间.本文将分享如何定义 ...
- react全局的公共组件-------弹框 (Alert)
最近研究react,发现写一个组件很容易,但是要写一个全局的好像有点麻烦.不能像VUE一样,直接在原型上面扩展,注册全局组件 下面实现一个弹框,只需要引入之后,直接调用方法即可,不需要写入组件 给出我 ...
- 如何优雅的写一个Vue 的弹框
写Vue或者是react 都会遇见弹框的问题.也尝试了多种办法来写弹框,一直都不太满意,今天特地看了一下 Element UI 的源码,模仿着写了一个简易版. 大概有一下几个问题: 1.弹框的层级问题 ...
- 从零开始写一个npm包,一键生成react组件(偷懒==提高效率)
前言 最近写项目开发新模块的时候,每次写新模块的时候需要创建一个组件的时候(包含组件css,index.js,组件js),就只能会拷贝其他组件修改名称 ,但是写了1-2个后发现效率太低了,而且极容易出 ...
随机推荐
- SpringMVC 解析(四)编程式路由
多数情况下,我们在使用Spring的Controller时,会使用@RequestMapping的形式把请求按照URL路由到指定方法上.Spring还提供了一种编程的方式去实现请求和路由方法之间的路由 ...
- 时间盲注——AS别名让盲注不盲
用处 页面存在时间盲注,注入成功了,你啥也看不到. 这只是为了能够查看到注入后的结果 网站部分源代码 <?php $conn = mysqli_("127.0.0.1",&q ...
- k8s pod 在迁移zookeeper时出现的问题
一次迁移中出现的问题,因为要搬迁机房,集群中的节点服务器分布在两个机房,通过专线打通了,现在需要整体都迁移到其中一个机房,所以pod要进行迁移,机器资源也比较紧张,在迁移中zookeeper迁移出现问 ...
- 【.NET6+Modbus】Modbus TCP协议解析、仿真环境以及基于.NET实现基础通信
前言:随着工业化的发展,目前越来越多的开发,从互联网走向传统行业.其中,工业领域也是其中之一,包括各大厂也都在陆陆续续加入工业4.0的进程当中. 工业领域,最核心的基础设施,应该是与下位硬件设备或程序 ...
- style设置 三个 竖线隔得太近了,这个属性和值 设置让他们分开一点
||| // html letter-spacing: 0.2em; // css
- HTML5+JavaScript+MUI 毕业答辩常见问题
问题1:HTML5前端开发环境有哪些? HTML5网页前端开发,实际也是HTML网页开发,HTML5知识万维网的核心语言.标准通用标记语言下的一个应用超文本标记语言(HTML)的第五次重大修改的第五次 ...
- Java语言学习day34--8月09日
##13Math类的方法_1 A:Math类中的方法 /* * static double sqrt(double d) * 返回参数的平方根 */ public static void functi ...
- 2021.08.03 BZOJ 疯狂的馒头(并查集)
2021.08.03 BZOJ 疯狂的馒头(并查集) 疯狂的馒头 - 题目 - 黑暗爆炸OJ (darkbzoj.tk) 重点: 1.并查集的神奇运用 2.离线化 题意: 给一个长为n的序列,进行m次 ...
- 论文解读(GCC)《Graph Contrastive Clustering》
论文信息 论文标题:Graph Contrastive Clustering论文作者:Huasong Zhong, Jianlong Wu, Chong Chen, Jianqiang Huang, ...
- 获取iframe的window对象
在父窗口中获取iframe中的元素 // JS // 方法1: var iframeWindow = window.frames["iframe的name或id"]; iframe ...