博客
关于我
封装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-http-flv-module流媒体服务器搭建+模拟推流+flv.js在前端html和Vue中播放HTTP-FLV视频流
查看>>
nginx-vts + prometheus 监控nginx
查看>>
Nginx/Apache反向代理
查看>>
Nginx: 413 – Request Entity Too Large Error and Solution
查看>>
nginx: [emerg] getpwnam(“www”) failed 错误处理方法
查看>>
nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:
查看>>
nginx: [error] open() “/usr/local/nginx/logs/nginx.pid“ failed (2: No such file or directory)
查看>>
nginx:Error ./configure: error: the HTTP rewrite module requires the PCRE library
查看>>
Nginx:objs/Makefile:432: recipe for target ‘objs/src/core/ngx_murmurhash.o‘解决方法
查看>>
nginxWebUI runCmd RCE漏洞复现
查看>>
nginx_rtmp
查看>>
Vue中向js中传递参数并在js中定义对象并转换参数
查看>>
Nginx、HAProxy、LVS
查看>>
nginx一些重要配置说明
查看>>
Nginx下配置codeigniter框架方法
查看>>
Nginx与Tengine安装和使用以及配置健康节点检测
查看>>
Nginx中使用expires指令实现配置浏览器缓存
查看>>
Nginx中使用keepalive实现保持上游长连接实现提高吞吐量示例与测试
查看>>
Nginx中如何配置WebSocket代理?
查看>>
Nginx中实现流量控制(限制给定时间内HTTP请求的数量)示例
查看>>