封装element-ui的dialog组件

2019-12-12  本文已影响0人  Yin先生

封装组件:

<template>
    <div>
         <el-dialog
            title="title"
            :visible.sync="visible"
            @close="click"
            :show="show">
            <span>this is a dialog</span>
          </el-dialog>
    </div>
</template>

<script>
 export default {
        data () {
            return {
                visible: this.show
            };
        },
        props: {
            show: {
                type: Boolean,
                default: false
            }
        },
        watch: {
            show () {
                this.visible = this.show;
            }
        },
        methods:{
        click() {
        this.$emit('update',this.show)
        }
        }
    };
</script>

使用封装的组件:

<template>
    <div>
       <service-dialog :show.sync="show" @update="btn"></service-dialog>
        <el-button type="primary" @click="open">click</el-button>
    </div>
</template>

<script>
import serviceDialog from './components/serviceDialog'
    export default {
        data () {
            return {
                show: false
            };
        },
        methods: {
            open () {
                this.show = true;
            },
            btn (payload) {
                this.show = !payload
            }
        },
        components: {
            serviceDialog
        }
    };
</script>
上一篇下一篇

猜你喜欢

热点阅读