美文网首页
封装分享组件

封装分享组件

作者: 董懂同学 | 来源:发表于2018-08-20 09:27 被阅读22次
h-share.gif

1. 引入相关的JS

 <script src="https://cdn.bootcss.com/vue/2.5.1/vue.min.js"></script>
<script src="/lib/vue-qrcode.min.js"></script>

2. 利用虚拟DOM封装组件

var VueQrcode = window.VueQrcode;
Vue.component('qrcode', VueQrcode);
Vue.component('h-share', {
    render: function (createElement) {
        var self = this;
        return createElement(
            'div',
            {
                style: {
                    display: 'inline-block'
                },
            },
            [
                createElement('i', {
                    attrs: {
                        class: 'el-icon-share'
                    },
                    style: {
                        marginRight: '10px'
                    }
                }, '分享'),
                createElement('span', {
                    attrs: {
                        class: 'icon-wechat'
                    },
                    style: {
                        marginRight: '5px'
                    },
                    on: {
                        click: function () {
                            window.ELEMENT.MessageBox({
                                title: '分享二维码',
                                center: true,
                                message: createElement('div', null, [
                                    createElement('qrcode', {
                                        props: {
                                            value: self.link,
                                            tag: 'img',
                                            options:{
                                                size: 150
                                            }
                                        }
                                    }, '')
                                ]),
                                showCancelButton: false,
                                showConfirmButton: false,
                                beforeClose: (action, instance, done) => {
                                    if (action === 'confirm') {
                                       console.log(action);
                                    } else {
                                        done();
                                    }
                                }
                            }).then(action => {
                                window.ELEMENT.Message({
                                    type: 'info',
                                    message: 'action: ' + action
                                });
                            }).catch(() => {});
                        }
                    }
                }, ''),
                createElement('span', {
                    attrs: {
                        class: 'icon-link'
                    },
                    on: {
                        click: function () {
                            window.ELEMENT.MessageBox({
                                title: '分享链接',
                                message: createElement('div', null, [
                                    createElement('el-input', {
                                        props: {
                                            value: self.link + "\n" + self.text,
                                            type: "textarea"
                                        },
                                        attrs: {
                                            id: 'input'
                                        },
                                    }, '')
                                ]),
                                showCancelButton: true,
                                confirmButtonText: '点击复制',
                                cancelButtonText: '取消',
                                beforeClose: (action, instance, done) => {
                                    if (action === 'confirm') {
                                        var input = document.getElementById("input");
                                        input.select(); // 选中文本
                                        document.execCommand("copy"); // 执行浏览器复制命令
                                        window.ELEMENT.Message({
                                            type:'success',
                                            message: '复制成功'
                                        });
                                    } else {
                                        done();
                                    }
                                }
                            }).then(action => {
                                window.ELEMENT.Message({
                                    type: 'info',
                                    message: 'action: ' + action
                                });
                            }).catch(() => {});
                        }
                    }
                }, '')
            ]
        )
    },
    attrs: {
        class: 'share-group'
    },
    props: {
        text: {
            type: String,
            required: true
        },
        link: {
            type: String,
            required: true
        }
    }
})

3. 如何使用组件

 <script src="/path/h-share.js"></script>
<h-share :text="text" :link="link" class="mr-20"></h-share>

相关文章

  • 封装分享组件

    1. 引入相关的JS 2. 利用虚拟DOM封装组件 3. 如何使用组件

  • 封装组件

    封装tab组件封装曝光加载组件封装轮播组件 代码

  • 面向对象实战

    封装轮播组件 轮播 封装曝光加载组件 曝光加载 封装Tab 组件 Tab组件

  • 面向对象实战

    1、tab组件封装2、轮播组件封装3、曝光组件封装

  • 封装element-ui的dialog组件

    封装组件: 使用封装的组件:

  • 高级-任务3

    封装一个轮播组件 封装一个曝光加载组件 封装一个 Tab 组件 封装一个 Modal 组件

  • 骨架屏(文章中间)

    分享 模板封装 组件封装(组件化) 骨架屏 小程序seo处理 对于在iphone系列下安全区踩的坑以及处理方式 百...

  • 面向对象实战

    题目1: 封装一个轮播组件轮播组件题目2: 封装一个曝光加载组件曝光加载组件题目3: 封装一个 Tab 组件Tab 组件

  • Web37.面向对象实战

    题目1:封装一个轮播组件 题目2:封装一个曝光加载组件 题目3:封装一个Tab组件 题目4:封装一个日历组件 题目...

  • element-ui 封装dialog组件

    封装组件: 使用组件:

网友评论

      本文标题:封装分享组件

      本文链接:https://www.haomeiwen.com/subject/ovkkiftx.html