当前位置:网站首页>实现弹框组件
实现弹框组件
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>
边栏推荐
- 浓眉大眼的谷歌 Chrome 也叛变了,教你一招快速清除其自带广告
- (C language) program environment and preprocessing
- NowCoderTOP17-22 二分查找/排序——持续更新ing
- Centos7 install mysql5.7
- KVM虚拟化作业
- GZIPInputStream 类源码分析
- Redis缓存面临的缓存穿透问题
- Sql optimization summary!detailed!(Required for the latest interview in 2021)
- C#之泛型、委托、事件及其使用
- IBM SPSS Statistics 28软件安装包下载及安装教程
猜你喜欢

SQL力扣刷题七

Day113. Shangyitong: user authentication, Alibaba Cloud OSS, patient management

ASP.NET 身份认证框架 Identity(一)

浓眉大眼的谷歌 Chrome 也叛变了,教你一招快速清除其自带广告

C#之泛型、委托、事件及其使用

sql中 exists的用法

unity-shader-2

Come n times - 07. Rebuild the binary tree

学习笔记——七周成为数据分析师《第二周:业务》:业务分析框架

Make your own dataset in FCN and train it
随机推荐
In half a month, MySQL has been consolidated again, and a tens of thousands of words "super hard core" article has been sorted out!
Summary of three methods for SQL deduplication
Detailed explanation of SQL stored procedures
SQL去重的三种方法汇总
【LeetCode】383.赎金信
Come n times with the sword--05. Replace spaces
Principle of Redis Sentinel
数据中台建设(六):数据体系建设
unity-shader-2
Source code analysis of GZIPInputStream class
SQLServer2019安装(Windows)
C#多态的实现
浅谈Attention与Self-Attention,一起感受注意力之美
前序、后序及层次遍历实现二叉树的序列化与反序列化
SQL如何从字符串截取指定字符(LEFT、MID、RIGHT三大函数)
Add a shuffling effect to every pie
富文本编辑器Tinymce
Web系统常见安全漏洞介绍及解决方案-sql注入
centos7安装mysql5.7
loadrunner录制问题