当前位置:网站首页>实现弹框组件
实现弹框组件
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>
边栏推荐
- Three ways of single sign-on
- Source code analysis of GZIPInputStream class
- 【LeetCode】36.有效的数独
- SQL——左连接(Left join)、右连接(Right join)、内连接(Inner join)
- Qt compile error: C2228: '.key' must have class/struct/union on the left
- 顺序表的删除
- NowCoderTOP28-34二叉树——持续更新ing
- The big-eyed Google Chrome has also betrayed, teach you a trick to quickly clear its own ads
- How SQL intercepts specified characters from strings (three functions of LEFT, MID, RIGHT)
- What is the encoding that starts with ?
猜你喜欢
我们能做出来数据库吗?
Redis缓存面临的缓存击穿问题
The big-eyed Google Chrome has also betrayed, teach you a trick to quickly clear its own ads
ASP.NET 身份认证框架 Identity(一)
浅谈Attention与Self-Attention,一起感受注意力之美
In half a month, MySQL has been consolidated again, and a tens of thousands of words "super hard core" article has been sorted out!
csdn文件导出为pdf
Centos7 install mysql5.7
尚医通【预约挂号系统】总结
cocoaPods管理之后工程结构变化
随机推荐
前序、后序及层次遍历实现二叉树的序列化与反序列化
细讲DDD领域驱动设计
【LeetCode】383.赎金信
SQL去重的三种方法汇总
Dart Log工具类
SQL存储过程详解
一种用于保证多方子系统数据一致性的方法
unity-shader-2
梅科尔工作室--鸿蒙十四天开发培训笔记(八)
强大的SQL计算利器-SPL
初识二叉搜索树
Web系统常见安全漏洞介绍及解决方案-sql注入
恋爱期间的赠与能否撤销
SQL力扣刷题五
Gradle系列——Groovy概述,基础使用(基于Groovy文档4.0.4)day2-1
项目管理工具之燃尽图:动态考核团队工作能力
踩水坑2 数据超出long long
【LeetCode】73.矩阵置零
windows平台下的mysql启动等基本操作
感情危机,朋友的网恋女友要和他闹分手,问我怎么办