Vue

Vue子组件的显示与隐藏

2018-09-17  本文已影响9人  Lia代码猪崽

一、定义一个子组件

<template>
    <div class="rules" v-show="showRules">
        <div class="rules-container">
            <div class="rules-inner">{{ rulesHTML }}</div>
        </div>
        <div class="rules-close" @click="close"></div>
    </div>
</template>
<script>
    export default {
        name: 'rules',
        props: {
            rulesHTML: {
                type: String,
                required: true
            },
            showRules: {
                type: Boolean,
                required: true
            }
        },
        methods: {
            close() {
                this.$emit('update:showRules', false)
            }
        }
    }
</script>

二、父组件中引用

<template>
    <div id="app" :style="{overflow: showRules ? 'hidden' : 'auto'}" >
        <div class="controls">
            <a class="rules" href="javascript: void(0)" @click="showRules = true"></a>
        </div>
    </div>
</template>
<script>
    import rules from '../components/rules'
    export default {
        name: 'App',
        components: {
            rules
        },
        data() {
            return {
                // 活动规则
                showRules: false,
                rulesHTML: ''dasdasd"
            }
        }
    }
</script>
上一篇下一篇

猜你喜欢

热点阅读