当前位置:网站首页>实现弹框组件
实现弹框组件
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>
边栏推荐
猜你喜欢

模块八

(C语言)程序环境和预处理

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

darknet 源码阅读笔记-01-activation_kernels.cu

odoo14 | 附件上传功能及实际使用

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

医院管理系统数据库,课程设计,SQLserver,纯代码设计

SQL学习笔记——REGEXP运算符

What does "chmod 777-R filename" mean?

In half a month, MySQL has been consolidated again, and a tens of thousands of words "super hard core" article has been sorted out!
随机推荐
The big-eyed Google Chrome has also betrayed, teach you a trick to quickly clear its own ads
感情危机,朋友的网恋女友要和他闹分手,问我怎么办
SQL存储过程详解
IBM SPSS Statistics 28软件安装包下载及安装教程
Chapter VII
SQLSERVER将子查询数据合并拼接成一个字段
如何判断自己是否适合IT行业?方法很简单
【LeetCode】Day108-和为 K 的子数组
KVM虚拟化作业
Business-(Course-Chapter-Subsection) + Course Publishing Some Business Ideas
(C语言)程序环境和预处理
Redis的简单使用
“chmod 777-R 文件名”什么意思?
Mybaits Frequently Asked Questions Explained
Mybaits 常用问题详解
Solve rpc error: code = Unimplemented desc = method CheckLicense not implemented
Chapter Six
Source code analysis of GZIPInputStream class
Summary of three methods for SQL deduplication
小程序如何使用订阅消息(PHP代码+小程序js代码)