Vue父组件控制子组件的显示

1,111 阅读1分钟
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./vue.js"></script>
</head>
<body>
    <div id="app">
        <button @click="showDiv()" >显示</button>
        <my-child v-show="bbb" @close="closeDiv()"></my-child>
    </div>
</body>
</html>
<script>
    var myChild={
        template:'<div>我的内容是可以切换的<button @click="closemyChild()">关闭</button></div>',
        data:function(){
            return{}
        },
        // props:['showif'],
        methods:{
            closemyChild(){
                this.$emit('close');
            }
        }
    }
    new Vue({
        el:'#app',
        components:{
            'my-child':myChild
        },
        data:function(){
            return{
                bbb:false
            }
        },
        methods:{
            showDiv:function(){
                this.bbb=true;
            },
            closeDiv:function(){
                this.bbb=false;
            }
        }
    })
</script>

思路:子组件点击按钮事件$emit父组件