vue封装命令式组件

在vue中,可以使用vue文件的组件,也可以使用js文件作为组件去调用。使用js组件例如:封装一个消息弹框确认组件,消息提醒组件等等;

vue2

import Vue from 'vue'
// import Vue from 'vue/dist/vue.esm.js'//使用template预编译,则引入此项
export function showMsg() {
    return new Promise((resolve, reject) => {
        const div = document.createElement('div');
        document.body.appendChild(div);
        div.setAttribute('id', 'dialog')
        new Vue({
            // 选项
            el: '#dialog', // 通常我们会在这里指定挂载点
            data: {
                title: '提示',
                content: '确定要关闭此页面?'
            },
            mounted() {
                this.$refs.dialogWindow.showModal();
            },
            render(h) {
                return h('dialog', { class: 'dialog-window', ref: "dialogWindow" }, [
                    h('div', { class: 'dialog-window-title' }, this.title),
                    h('div', { class: 'dialog-window-content' }, this.content),
                    h('div', { class: 'dialog-window-button' }, [
                        h('div', {
                            class: 'dialog-window-cancel', on: {
                                click: this.cancelDialog
                            }
                        }, '取消'),
                        h('div', {
                            class: 'dialog-window-close', on: {
                                click: this.closeDialog
                            }
                        }, '关闭')
                    ])
                ])
            },
            methods: {
                closeDialog() {
                    this.$refs.dialogWindow.close();
                    let body = document.getElementsByTagName('body')[0]
                    let dialog = document.getElementsByClassName('dialog-window')[0];
                    body.removeChild(dialog)
                    resolve()
                },
                cancelDialog() {
                    this.$refs.dialogWindow.close();
                    let body = document.getElementsByTagName('body')[0]
                    let dialog = document.getElementsByClassName('dialog-window')[0];
                    body.removeChild(dialog)
                    reject()
                }
            }
        });
    })
}

vue3

import { createApp } from 'vue'
export function messages() {
    return new Promise((resolve, reject) => {
        const div = document.createElement('div');
        document.body.appendChild(div);
        div.setAttribute('id', 'dialog')
        const app = createApp({
            data() {
                return {
                    title: '提示',
                    content: '确定要关闭此页面?'
                }

            },
            mounted() {
                this.$refs.dialogWindow.showModal();
            },
            render() {
                // jsx语法
                return (<dialog class="dialog-window" ref="dialogWindow">
                    <div class="dialog-window-title">{this.title}</div>
                    <div class="dialog-window-content">{this.content}</div>
                    <div class="dialog-window-button">
                        <div class="dialog-window-cancel" onClick={() => this.cancelDialog()}>取消</div>
                        <div class="dialog-window-close" onClick={() => this.closeDialog()}>关闭</div>
                    </div>
                </dialog>)
            },
            methods: {
                closeDialog() {
                    app.unmount(div);
                    div.remove()
                    resolve()
                },
                cancelDialog() {
                    app.unmount(div);
                    div.remove()
                    reject()
                }
            }
        });
        app.mount(div)
    })
}

将此文件引入到main.js或者需要使用的vue文件上,就可以使用
想要在文件在写上css ,可以使用css in js

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值