vue中如何用纯js调用组件

项目中有这样的需求
创建任务的抽屉需要在多个页面中调用,如果每个页面都要写import然后写html啥的很麻烦,所以决定模仿组件库的this.$message,这种一行调用的方法。
主要功能如下:
1.能给抽屉传值
2.抽屉关闭后有callback函数,方便刷新主页面数据

尝试了两种写法,方法一,使用install
代码如下:
drawer.vue

<template>
    <el-drawer title="我是外面的 Drawer" :visible.sync="drawer" size="50%" @closed="closed(222)">
        <div>
            <el-button @click="innerDrawer = true">打开里面的!</el-button>
            <el-drawer title="我是里面的" :append-to-body="true" :visible.sync="innerDrawer">
                <p>_(:зゝ∠)_</p>
            </el-drawer>
        </div>
    </el-drawer>
</template>

<script>
    export default {
        props: {
            drawer: {
                type: Boolean,
                default: false
            }
        },
        data() {
            return {
                innerDrawer: false
            };
        },
        methods: {
            closed(val) {
                this.callback && this.callback(val)
            }
        }
    };
</script>

index.js

import drawerVue from "./drawer.vue"

const showDrawer = {};

showDrawer.install = function (Vue, options = {}) {
    const DrawerConstructor = Vue.extend(drawerVue);
    let currentMsg;
    const initInstance = () => {
        currentMsg = new DrawerConstructor({});
        document.body.appendChild(currentMsg.$mount().$el);
    };
    Vue.prototype.$showDrawer = {
        showDrawer(options) {
            if (!currentMsg) {
                initInstance();
            }
            if (typeof options === 'object') {
                Object.assign(currentMsg, options);
            }
        }
    };
};
export default showDrawer;
  

main.js

import showDrawer from './components/common/drawer/index.js';
Vue.use(showDrawer);

调用

<template>
    <div>
        <el-button @click="handleClick" type="primary" style="margin-left: 16px;">
            点我打开
        </el-button>
        <el-button @click="handleClick" type="primary" style="margin-left: 16px;">
            点我打开2
        </el-button>
    </div>
</template>
<script>
    export default {
        methods: {
            handleClick() {
                this.$showDrawer.showDrawer({
                    drawer: true,
                    callback: val => {
                        console.log(val, 111) //这里会打印出222,111
                    }
                })
            }
        }
    }
</script>

不使用install的方法二
代码如下:
drawer.vue

<template>
    <el-drawer title="我是外面的 Drawer" :visible.sync="drawer" size="50%" @closed="closed(222)">
        <div>
            <el-button @click="innerDrawer = true">打开里面的!</el-button>
            <el-drawer title="我是里面的" :append-to-body="true" :visible.sync="innerDrawer">
                <p>_(:зゝ∠)_</p>
            </el-drawer>
        </div>
    </el-drawer>
</template>

<script>
    export default {
        data() {
            return {
                innerDrawer: false,
                drawer: false
            };
        },
        methods: {
            closed(val) {
                this.callback && this.callback(val)
            }
        }
    };
</script>

index.js

import Vue from "vue";
import drawerVue from "./drawer.vue"

const showDrawer = (options = {}) => {
    const DrawerConstructor = Vue.extend(drawerVue);
    let data = {}, methods = {};
    Object.keys(options).forEach(e => {
        if (typeof options[e] === 'function') {
            methods[e] = options[e];
        } else {
            data[e] = options[e];
        }
    })
    let instance = new DrawerConstructor({
        data: data,
        methods: methods
    });
    document.body.appendChild(instance.$mount().$el);
}
export default showDrawer;

main.js

import showDrawer from './components/common/drawer/index.js';
Vue.prototype.$showDrawer=showDrawer

调用

<template>
    <div>
        <el-button @click="handleClick" type="primary" style="margin-left: 16px;">
            点我打开
        </el-button>
        <el-button @click="handleClick" type="primary" style="margin-left: 16px;">
            点我打开2
        </el-button>
    </div>
</template>
<script>
    export default {
        methods: {
            handleClick() {
                this.$showDrawer({
                    drawer: true,
                    callback: val => {
                        console.log(val, 111)
                    }
                })
            }
        }
    }
</script>

个人更倾向于用方法二,因为本菜鸡不知道方法一怎么实现局部注册
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值