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

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

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

// 创建指定组件实例并挂载于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组件

使用组件的地方

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

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

你可能感兴趣的文章
Nginx 入门,一篇搞定!
查看>>
Nginx 利用代理转发请求示例
查看>>
Nginx 动静分离与负载均衡的实现
查看>>
Nginx 反向代理 MinIO 及 ruoyi-vue-pro 配置 MinIO 详解
查看>>
nginx 反向代理 转发请求时,有时好有时没反应,产生原因及解决
查看>>
Nginx 反向代理+负载均衡
查看>>
Nginx 反向代理解决跨域问题
查看>>
Nginx 反向代理配置去除前缀
查看>>
nginx 后端获取真实ip
查看>>
Nginx 多端口配置和访问异常问题的排查与优化
查看>>
Nginx 如何代理转发传递真实 ip 地址?
查看>>
Nginx 学习总结(16)—— 动静分离、压缩、缓存、黑白名单、性能等内容温习
查看>>
Nginx 学习总结(17)—— 8 个免费开源 Nginx 管理系统,轻松管理 Nginx 站点配置
查看>>
Nginx 学习(一):Nginx 下载和启动
查看>>
nginx 常用指令配置总结
查看>>
Nginx 常用配置清单
查看>>
nginx 常用配置记录
查看>>
nginx 开启ssl模块 [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx
查看>>
Nginx 我们必须知道的那些事
查看>>
Nginx 源码完全注释(11)ngx_spinlock
查看>>