vue 组件父子孙 三代传递 以及 子传父 孙传父

 

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>父子孙相互通信</title>
    <style>
        td {
            border: 1px solid black;
            text-align: center;
        }
    </style>
</head>
<script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
<body>
<section id="app">
    <table border="1" cellspacing="0" width="600px">
        <tr>
            <th colspan="3">父亲的数据</th>
        </tr>
        <tr>
            <td>name</td>
            <td>{{name}}</td>
            <td><input type="text" v-model="name"></td>
        </tr>
        <tr>
            <td>age</td>
            <td>{{age}}</td>
            <td><input type="text" v-model="age"></td>
        </tr>
    </table>
    <v-Child :fn="name" :fa="age" @childbyvalue="childByValue"></v-Child>
</section>


<template id="child">
    <div>
        <button @click="change">子传父</button>
        <table border="1" cellspacing="0" width="600px">
            <tr>
                <th colspan="3">儿子的数据</th>
            </tr>
            <tr>
                <td>Myname</td>
                <td>{{cn}}</td>
                <td><input type="text" v-model="cn"></td>
            </tr>
            <tr>
                <td>Myage</td>
                <td>{{ca}}</td>
                <td><input type="text" v-model="ca"></td>
            </tr>
        </table>
        <v-son :sn="cn" :sa="ca"></v-son>
    </div>
</template>

<template id="son">
    <div>
        <button @click="sonchange">孙子传父</button>
        <table border="1" cellspacing="0" width="600px">
            <tr>
                <th colspan="3">孙子的数据</th>
            </tr>
            <tr>
                <td>Myname</td>
                <td>{{wn}}</td>
                <td><input type="text" v-model="wn"></td>
            </tr>
            <tr>
                <td>Myage</td>
                <td>{{wa}}</td>
                <td><input type="text" v-model="wa"></td>
            </tr>
        </table>
    </div>
</template>
</body>
<script>
    let app1=new Vue({})                   //  过度
    let app = new Vue({
        // el: "#app",
        data: {
            name: "father",
            age: 20
        },
        methods: {
            childByValue(childValue) {
                this.name = childValue.cname
                this.age = childValue.cage
            },
        },
        mounted(){
            let that=this
            app1.$on("sonbyvalue",function (val) {           //获取页面中数据  绑定
                that.name=val.cname
                that.age=val.cage
            })
        },
        components: {
            "vChild": {
                props: ["fn", "fa"],               //接收数据
                template: "#child",
                data: function () {             //每接收一个,返回一次,时时更新
                    return {
                        cn: this.fn,
                        ca: this.fa
                    }
                },
                watch: {                          //监听 数据改变
                    fn: function () {
                        this.cn = this.fn;
                    },
                    fa: function () {
                        this.ca = this.fa;
                    }
                },
                methods: {
                    change() {
                        let obj = {                        //数据写入对象
                            cname: this.cn,
                            cage: this.ca
                        }
                        this.$emit("childbyvalue", obj)      //触发    发送数据
                    }
                },
                components: {
                    "vSon": {
                        props: ["sn", "sa"],                    //接收数据
                        template: "#son",
                        methods:{
                            sonchange(){
                                let obj1={                              //数据写入对象
                                    cname: this.wn,
                                    cage:this.wa
                                }
                                app1.$emit("sonbyvalue",obj1)       //触发    发送数据
                            }
                        },
                        data: function () {                 //时时更新接收数据
                            return {
                                wn: this.sn,
                                wa: this.sa
                            }
                        },
                        watch: {                             //监听 数据改变
                            sn: function () {
                                this.wn = this.sn;
                            },
                            sa: function () {
                                this.wa = this.sa;
                            }
                        }
                    }
                }
            }
        }
    }).$mount('#app')
</script>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值