当前位置:网站首页>实现弹框组件
实现弹框组件
2022-07-31 10:24:00 【yibucuo】
弹窗这类组件的特点是它们在当前vue实例之外独立存在,通常挂载于body;它们是通过JS动态创建的,不需要在任何组件中声明。
常见使用方法是:
this.$create(Notice, {
title: '这是标题内容',
message: '这是信息内容',
duration: 1000
}).show();
使用render创建组件实例
import Vue from "vue";
function create(Component, props) {
const vm = new Vue({
render(h) {
// render函数将传入组件配置对象转换为虚拟dom
return h(Component, {
props });
}
}).$mount(); //执行挂载函数,但未指定挂载目标,表示只执行初始化工作
// 将生成dom元素追加至body
document.body.appendChild(vm.$el);
// 给组件实例添加销毁方法
const comp = vm.$children[0];
comp.remove = () => {
document.body.removeChild(vm.$el);
vm.$destroy();
};
return comp;
}
// 暴露调用接口
export default create;
使用Vue.extend方式创建组件实例
import Vue from "vue";
function create(Component, props) {
const Ctor = Vue.extend(Component)
const comp = new Ctor({
propsData:props})
comp.$mount()
document.body.appendChild(comp.$el);
comp.remove = () => {
document.body.removeChild(comp.$el);
comp.$destroy();
};
return comp;
}
export default create;
实现通知组件,Notice.vue
<template>
<div class="box" v-if="isShow">
<h3>{
{ title }}</h3>
<p class="box-content">{
{ message }}</p>
</div>
</template>
<script> export default {
props: {
title: {
type: String, default: "", }, message: {
type: String, default: "", }, duration: {
type: Number, default: 1000, }, }, data() {
return {
isShow: false, }; }, methods: {
show() {
this.isShow = true; setTimeout(this.hide, this.duration); }, hide() {
this.isShow = false; this.remove(); }, }, }; </script>
<style> .box {
position: fixed; width: 100%; top: 16px; left: 0; text-align: center; pointer-events: none; background-color: #fff; border: grey 3px solid; box-sizing: border-box; } .box-content {
width: 200px; margin: 10px auto; font-size: 14px; padding: 8px 16px; background: #fff; border-radius: 3px; margin-bottom: 8px; } </style>
使用测试
<script>
import create from "@/utils/create";
import Notice from "@/components/Notice";
export default {
methods: {
submitForm(form) {
this.$refs[form].validate((valid) => {
const notice = create(Notice, {
title: "这是标题内容",
message: valid ? "请求登录!" : "校验失败!",
duration: 1000,
});
notice.show();
});
},
},
};
</script>
边栏推荐
- In half a month, MySQL has been consolidated again, and a tens of thousands of words "super hard core" article has been sorted out!
- Redis缓存面临的缓存击穿问题
- Sql optimization summary!detailed!(Required for the latest interview in 2021)
- 细讲DDD领域驱动设计
- Echart饼图添加轮播效果
- ASP.NET 身份认证框架 Identity(一)
- 【LeetCode】21. 合并两个有序链表
- GZIPInputStream 类源码分析
- 医院管理系统数据库,课程设计,SQLserver,纯代码设计
- Come n times with the sword--05. Replace spaces
猜你喜欢
随机推荐
第七章
业务-(课程-章节-小节)+课程发布一些业务思路
MySQL中JOIN的用法
unity-shader-2
项目管理工具之燃尽图:动态考核团队工作能力
SQL——左连接(Left join)、右连接(Right join)、内连接(Inner join)
Redis缓存面临的缓存穿透问题
The big-eyed Google Chrome has also betrayed, teach you a trick to quickly clear its own ads
darknet 源码阅读笔记-01-activation_kernels.cu
Redis缓冲穿透和缓冲击穿工具类的封装
Gradle series - Groovy overview, basic use (based on Groovy document 4.0.4) day2-1
感情危机,朋友的网恋女友要和他闹分手,问我怎么办
NowCoderTOP23-27二叉树遍历——持续更新ing
【LeetCode】36.有效的数独
The fifth chapter
浅谈Attention与Self-Attention,一起感受注意力之美
&#x开头的是什么编码?
VMware下安装win10启动后进入Boot Manger界面如何解决
Sql优化总结!详细!(2021最新面试必问)
Build finished with errors/Executable Not Found









