Vue自定义组件

1.在component文件夹中创建组件confirm.vue

<template>
    <div class="mask">
        <div class="confirm-wrap">
            <div class="title">确认删除?</div>
            <div class="handel">
                <div class="btn">取消</div>
                <div class="btn">确认</div>
            </div>
        </div>
    </div>
</template> 

<style lang="less" scoped>
.mask{
    width: 100%;
    height: 100%;
    position: fixed;
    z-index: 99;
    background-color: rgba(0,0,0,0.6);
    left: 0;
    top: 0;
    .confirm-wrap{
        width: 250px;
        height: 120px;
        background-color: #fff;
        position: absolute;
        border-radius: 10px;
        z-index: 1;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
        .title{
            text-align: center;
            font-size: 14px;
            margin-top: 20px;
        }
        .handel{
            display: flex;
            justify-content: space-between;
            margin-top:20px;
            border-top: 1px solid #eee;
            .btn{
                width: 100%;
                text-align: center;
                padding: 18px;
            }
            .btn:first-child{
                color: #868686;
                border-right: 1px solid #eee;
            }
        }
    }
}
</style>

2.在component文件夹创建index.js

import Confirm from './confirm';
export default {
    install(Vue){
        Vue.component("my-confirm",Confirm)
        // let ConfirmObj = Vue.extend(Confirm)
        // Vue.prototype.$confirm = function(msg, btns){
        //     let ConfirmInit = new ConfirmObj().$mount(document.createElement("div"))
        //     console.log(ConfirmInit)
        //     document.body.appendChild(ConfirmInit.$el)
        //     ConfirmInit.msg = msg
        //     ConfirmInit.btns = btns
        //     console.log(msg, btns)
        // }
    }
}

3.在入口的main.js文件中如下配置:

import Confirm from './component';
Vue.use(Confirm)

4.在需要使用的组件中正常调用

<template>
    <div id="bg">
        <!-- <button @click="del">删除</button> -->
        <my-confirm></my-confirm>
    </div>
</template>
自定义组件
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容