【Vue】兄弟组件传值+父向子传值+子向父传值+父调子方法属性

本文详细介绍了Vue框架中组件间的通信方式,包括父组件向子组件传递数据使用props,子组件通过$emit向父组件传递值,以及利用$on进行兄弟组件之间的通信。示例代码展示了具体的实现过程,包括属性定义、事件监听和触发等操作。
摘要由CSDN通过智能技术生成

前言:
vue框架中的主要传值方式有3种,父组件向子组件传值,子组件向父组件传值,以及兄弟组件间传值,下面分别介绍下这三种传值情况。

【父组件向子组件传值—通过props】

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" />
        <meta name="format-detection" content="telephone=no,email=no,date=no,address=no"/>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
        <title>父组件向子组件传值</title>
        <style>
            [v-cloak]{
                display: none;
            }
        </style>
    </head>
    <body>
        <div id="app" v-cloak>
        </div>
        <script src="../js/vue.js"></script>
        <script>
            //子组件
            let ChildrenComponent={
                // props:["post-title","sub-title"],
                props:{
                    "post-title":{
                        type:String,
                        default:'123'
                    },
                    "sub-title":{
                        type:String,
                        default:''
                    }
                },
                template:`
                    <div>{{postTitle}}</div>
                `
            };
            //父组件,// <ChildrenComponent></ChildrenComponent>
            let ParentComponent={
                data(){
                    return{
                        title:'我是父组件的title',
                        subTitle:'我是父组件的subTitle'
                    }
                },
                template:`
                    <div>
                        <children-component :post-title="title" :sub-title="subTitle"></children-component>
                    </div>
                `,
                components:{
                    ChildrenComponent
                }
            }
            //常用
            new Vue({
                el:"#app",
                components:{
                    ParentComponent
                },
                template:`<parent-component></parent-component>`
            });
        </script>
    </body>
</html>

【子组件向父组件传值—$emit和ref】

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" />
        <meta name="format-detection" content="telephone=no,email=no,date=no,address=no"/>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
        <title>子组件向父组件传值</title>
        <style>
            [v-cloak]{
                display: none;
            }
        </style>
    </head>
    <body>
        <div id="app" v-cloak>
        </div>
        <script src="../js/vue.js"></script>
        <script>
            //子组件
            let ChildrenComponent={
                template:`
                    <button type="button" @click="toValue">子组件向父组件传值</button>
                `,
                methods:{
                    toValue(){
                        this.$emit('handlerClick',"我是子组件值1","我是子组件值2");
                    }
                }
            };
            //父组件,// <ChildrenComponent></ChildrenComponent>
            let ParentComponent={
                data(){
                    return{
                        title1:"",
                        title2:""
                    }
                },
                template:`
                    <div>
                        <children-component @handlerClick="getValue"></children-component>
                        {{title1}}--{{title2}}
                    </div>
                `,
                components:{
                    ChildrenComponent
                },
                methods:{
                    getValue(val1,val2){
                        this.title1 = val1;
                        this.title2 = val2;
                    }
                }
            }
            //常用
            new Vue({
                el:"#app",
                components:{
                    ParentComponent
                },
                template:`<parent-component></parent-component>`
            });
        </script>
    </body>
</html>

【兄弟组件传值----通过$ emit和$on】

**前言:**传值的兄弟组件使用$ emit,接受值的兄弟组件使用$ on,都需要先let vue = new Vue();再vue.$ emit(),vue.$on()

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" />
        <meta name="format-detection" content="telephone=no,email=no,date=no,address=no"/>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
        <title>兄弟组件间传值</title>
        <style>
            [v-cloak]{
                display: none;
            }
        </style>
    </head>
    <body>
        <div id="app" v-cloak>
        </div>
        <script src="../js/vue.js"></script>
        <script>
            let vue = new Vue();
            //A组件
            let AComponent={
                template:`
                    <button type="button" @click="toValue">A组件给B组件传值</button>
                `,
                methods:{
                    toValue(){
                        vue.$emit('handlerClick',"我是子组件值1","我是子组件值2");
                    }
                }
            };
            //B组件
            let BComponent={
                data(){
                    return{
                        title1:"",
                        title2:""
                    }
                },
                template:`
                    <div>
                        {{title1}}--{{title2}}
                    </div>
                `,
                created(){
                    vue.$on("handlerClick",(val1,val2)=>{
                        this.title1 = val1;
                        this.title2 = val2;
                    })
                }
            }
            //常用
            new Vue({
                el:"#app",
                components:{
                    AComponent,
                    BComponent
                },
                template:`
                    <div>
                        <AComponent></AComponent>
                        <BComponent></BComponent>
                    </div>
                `
            });
        </script>
    </body>
</html>

【父组件调用子组件的方法属性----ref】

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" />
        <meta name="format-detection" content="telephone=no,email=no,date=no,address=no"/>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
        <title>父组件调用子组件的方法,通过ref</title>
        <style>
            [v-cloak]{
                display: none;
            }
        </style>
    </head>
    <body>
        <div id="app" v-cloak>
        </div>
        <script src="../js/vue.js"></script>
        <script>
            //子组件
            let ChildComponent={
                props:{
                    value:String
                },
                template:`
                    <div>
                        <div>子组件</div>
                    </div>
                `,
                methods:{
                    send(){
                        console.log("父组件调用子组件的方法22222");
                    }
                }
            };
            //父组件
            let ParentComponent={
                data(){
                    return{
                    }
                },
                template:`
                    <div>
                        <ChildComponent ref="childValues"></ChildComponent>
                        <button type="button" @click="getValue">父组件调用子组件的方法</button>
                    </div>
                `,
                components:{
                    ChildComponent
                },
                methods:{
                    getValue(){
                        //方式一 
                        // this.$refs.childValues.send();
                        //方式二
                        this.$refs['childValues'].send();
                    }
                }
            }
            //常用
            new Vue({
                el:"#app",
                components:{
                    ParentComponent
                },
                template:`
                    <div>
                        <ParentComponent></ParentComponent>
                    </div>
                `
            });
        </script>
    </body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值