• bus案例


    <!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>
        <style>
            .item {
                overflow: hidden;
                padding: 10px;
                 400px;
                border:1px solid red;
            }
    
            .item img {
                 100px;
                float: left;
    
            }
            .film{
                border:1px solid black;
                height: 1500px;
            }
    
            .filminfo {
                 300px;
                min-height: 200px;
                background: yellow;
                position: fixed;
                right:50px;
                top:100px;
                border:1px solid blue;
            }
        </style>
        <script src="lib/vue.js"></script>
    
    </head>
    
    <body>
        <div id="box">
            <button @click="handleAjax">ajax</button>
            
            <film-item v-for="item in datalist" :key="item.filmId" :mydata="item"></film-item>
    
            <film-detail ></film-detail>
        </div>
        <script>
    
            var bus = new Vue()
            // bus.$on
            // bus.$emit
            Vue.component("filmItem",{
                props:["mydata"],
                template:`
                    <div class="item">
                        <img :src="mydata.poster"/>
                        {{mydata.name}}
                        <div>
                            <button @click="handleClick">详情</button>
                        </div>
                    </div>
                `,
                methods:{
                    handleClick(){
                        // console.log(this.mydata.synopsis)
    
                        bus.$emit("kerwin",this.mydata.synopsis)
                    }
                }
            })
    
            Vue.component("filmDetail",{
                //组件刚刚创建好,就开始订阅
                data(){
                    return {
                        info:""
                    }
                },
                //生命周期
                mounted(){
                    // console.log("当前组件上树后触发")
                    bus.$on("kerwin",(data)=>{
                        console.log("1111111",data)
                        this.info = data
                    })
                },
    
                template:`
                    <div class="filminfo">
                        {{info}}
                    </div>  
                `
            })
    
    
    
            new Vue({
                el:"#box",
                data:{
                    datalist:[],
                },
                methods:{
                    handleAjax(){
                        fetch("./json/test.json")
                            .then(res=>res.json())
                            .then(res=>{
                                console.log(res.data.films)
                                this.datalist = res.data.films
                            })
                    }
                }
            })
           
        </script>
    </body>
    
    </html>
  • 相关阅读:
    JavaScript autoComplete 1.2
    Linux下安装配置git
    《SQL Server 2008从入门到精通》20180627
    《SQL必知必会》知识点汇总
    关于js的setTimeout和setInterval
    关于js的闭包
    web图片类型
    关于绑定事件
    js原型与继承
    关于js的mouseout
  • 原文地址:https://www.cnblogs.com/yyy1234/p/16063464.html
Copyright © 2020-2023  润新知