博客
关于我
封装vue的弹窗组件
阅读量:319 次
发布时间:2019-03-04

本文共 1559 字,大约阅读时间需要 5 分钟。

先写一个工具函数,创建组件实例

// 创建指定组件实例并挂载于body上import Vue from 'vue';export default function create(Component, props) {       // 0. 先创建vue实例    const vm = new Vue({           render(h) {               // render方法提供给我们一个h函数,它可以渲染VNode            return h(Component, {   props})        }    }).$mount(); // 更新操作        // 1. 上面vm帮我们创建组件实例    // 2. 通过$children获取该组件实例    console.log(vm.$root);        const comp = vm.$children[0];    // 3. 追加至body    document.body.appendChild(vm.$el);    // 4. 清理函数    comp.remove = () => {           document.body.removeChild(vm.$el);        vm.$destroy();    }    // 5. 返回组件实例    return comp;}

Notice组件

<template>  <div v-if="isShow">    <h3>{   {   title}}</h3>    <p>{   {   message}}</p>  </div></template><script>export default {     props: {       title: {         type: String,      default: ""    },    message: {         type: String,      default: ""    },    duration: {         type: Number,      default: ""    }  },  data() {       return {         isShow: false    };  },  methods: {       show() {         this.isShow = true;      setTimeout(() => {             this.hide()      }, this.duration);    },    hide() {         this.isShow = false;      this.remove();    }  }};</script><style lang="scss" scoped></style>

使用组件的地方

 methods: {       onLogin() {         // 创建弹窗实例      let notice;      this.$refs.loginForm.validate(isValid => {           notice = create(Notice, {             title: "xxx",          message: isValid ? "登录!!!" : "有错!!!",          duration: 10000        });        notice.show();      });    }  }

转载地址:http://tvsh.baihongyu.com/

你可能感兴趣的文章
$set的使用(视图不能实时更新)
查看>>
Spring知识小汇(6)——Bean的自动装配
查看>>
一、硬件防火墙
查看>>
Javaweb jQuery功能练习
查看>>
余生,愿你能靠近那些正能量的人——
查看>>
初学QT
查看>>
IOC容器_Bean管理xml方式
查看>>
蓝桥杯入门练习题斐波那契数列
查看>>
(Java基础类库 )System类
查看>>
context:include-filter与exclude-filte控制扫描组件
查看>>
【SSL】1072砝码称重
查看>>
js数据结构--队列--常见操作
查看>>
JS数据结构--单向链表--常见操作
查看>>
【SSL】1606&【洛谷】P2014选课
查看>>
JS数据结构--双向链表--常见操作
查看>>
c++的内存管理
查看>>
全排列(深度优先搜索+递归)
查看>>
多项式插值法的Python程序
查看>>
vue.js常用指令及用法
查看>>
vuex的核心概念和运行机制
查看>>