弹窗练习

弹窗练习

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>插槽</title>
    //写弹窗的样式
     <style>
        *{
            margin:0;
            padding:0;
        }
        .mask{
            position:fixed;
            width:100%;
            height:100%;
            left:0;
            top:0;
            background-color:rgba(0,0,0,.35);
        }
        .mask .show{
            position: absolute;
            left:0;
            right:0;
            top:0;
            bottom: 0;
            width:600px;
            height:350px;
            margin:auto;
            background: #fff;
        }
        .mask .show button{
            position: absolute;
            right:0;
        }
    </style>
</head>
<body>
//定义一个父子件
<div id="app" >
    <button @click="fn">点击弹窗</button>
    <hide v-if="isShow" @xxx="close"></hide>//用isshow来判断是否显示弹窗的样式
</div>
//定义一个子组件
<template id="hide">
    <div class="mask">
        <div class="show">
            <button @click="fn">关闭弹窗</button>
        </div>
    </div>
</template>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

<script>

    let hide = {
        template:'#hide',
        methods:{
            fn(){   //向父组件发射xxx事件
                this.$emit('xxx')
            }
        }
    }
    let vm = new Vue({
        el:'#app',
        data:{
            isShow:false,  //控制弹窗显示
        },
        components:{
            hide
        },
        methods:{
            fn(){
                this.isShow = true;
            },
            close(){
                this.isShow = false;
            }
        }
    })
</script>
</body>
</html>

eventBus

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        .mask{
            position:fixed;
            width:100%;
            height:100%;
            left:0;
            top:0;
            background-color:rgba(0,0,0,.35);
        }
        .mask .show{
            position: absolute;
            left:0;
            right:0;
            top:0;
            bottom: 0;
            width:600px;
            height:350px;
            margin:auto;
            background: #fff;
        }
        .mask .show button{
            position: absolute;
            right:0;
        }
    </style>
</head>
<body>

<div id="app" >
    <heaven></heaven>
    <xiaoming></xiaoming>
</div>


<template id="heaven">
    <div @click="fn"> {{msg}}</div>
</template>

<template id="xiaoming">
    <div @click="fn"> {{msg}} </div>
</template>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>

    let eventBus = new Vue();//相当于设置一个全局的组件
    let heaven = {
        template:'#heaven',
        data(){
            return {
                msg:'我是heaven'
            }
        },
        created(){
            eventBus.$on('xxx',(msg)=>{   //heaven组件先订阅一个事件
                console.log('xxx事件已经触发了  我监控到了',msg)
            });
        },
        methods:{
            fn(){
                eventBus.$emit('yyy',this.msg)
            }
        }
    }
    let xiaoming = {
        template:'#xiaoming',
        data(){
            return {
                msg:'我是xiaoming'
            }
        },
        created(){
            eventBus.$on('yyy',(msg)=>{
                console.log('yyy事件已经触发了  我监控到了',msg)
            });
        },
        methods:{
            fn(){
                eventBus.$emit('xxx',this.msg)
            }
        }
    }
    let vm = new Vue({
        el:'#app',
        components:{
            heaven,
            xiaoming
        }

    })
</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值