Vue_(组件通讯)父组件向子组件传值

  

  Vue组件  传送门

  父组件向子组件传值:父组件通过属性向下传值的方式和子组件通信;

  使用步骤:

    1、定义组件:现有自定义组件com-a、com-b,com-a是com-b的父组件

    2、准备获取数据:com-b要获取父组件data中的name属性

    3、在<com-b :name=“name”></com-b> 使用v-bind 绑定name属性,红色部分为属性名称,可以随意写

    4、在子组件定义部分里添加选项,值是个字符串数组 props:[‘name’],将上边红色的属性名称写在这里

    5、之后就可定义在子组件中使用name属性了

  Learn:

    一、父组件向子组件传值 

    二、Props选项高级选项配置 

  目录结构

  

一、父组件向子组件传值

  需要在父组件中用v-bind绑定name属性

<template id="father-template">
        <div>
            <h1>father component</h1>
            myData : <span>{{name}}</span><br />
            <input type="text" v-model="name"/>
            <hr />

            <child-component :name="name"></child-component>
        </div>
    </template>
    

  在Vue中components属性中通过props选项给子组件绑定name标签

        new Vue({
            data : {

            },
            components : {
                "father-component" : {
                    data(){
                        return {
                            name : ‘Gary‘
                        }
                    },
                    template :  "#father-template",
                    components : {
                        "child-component" : {
                            template : "#child-template",
                            props:[‘name‘]
                        }
                    }
                }
            }
        }).$mount("#GaryId");

  在子组件中就可以直接通过{{props}}将值传递过去

    <template id="child-template">
        <div>
            <h2>child component</h2>
            fatherData : <span>{{name}}</span>
        </div>
    </template>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Gary</title>
    </head>
    <body>
        <div id="GaryId">
            <father-component></father-component>
        </div>
    </body>

<template id="father-template">
        <div>
            <h1>father component</h1>
            myData : <span>{{name}}</span><br />
            <input type="text" v-model="name"/>
            <hr />

            <child-component :name="name"></child-component>
        </div>
    </template>

    <template id="child-template">
        <div>
            <h2>child component</h2>
            fatherData : <span>{{name}}</span>
        </div>
    </template>

    <script type="text/javascript" src="../js/vue.js" ></script>
    <script type="text/javascript">

        new Vue({
            data : {

            },
            components : {
                "father-component" : {
                    data(){
                        return {
                            name : ‘Gary‘
                        }
                    },
                    template :  "#father-template",
                    components : {
                        "child-component" : {
                            template : "#child-template",
                            props:[‘name‘]
                        }
                    }
                }
            }
        }).$mount("#GaryId");

    </script>
</html>

Gary_fatherAndChild.html

  在father组件的<child-component>组件中使用多个v-bind属性可实现多组数值传递

<template id="father-template">
        <div>
            <h1>father component</h1>
            myData : <span>{{name}},{{id}},{{user.username}}</span><br />
            fatherDate : <span>msg</span><br />
            <input type="text" v-model="name"/>
            <hr />

            <child-component :name="name" :id="id" :user="user"></child-component>
        </div>
    </template>

  Vue中给子组件添加user对象,以及给对象初始值msg:‘helloVue‘并在父组件中通过<father-component :msg="msg"></father-component>中直接使用

        new Vue({
            data : {
                 msg:‘helloVue‘
            },
            components : {
                "father-component" : {
                    data(){
                        return {
                            id:1,
                            name : ‘Gary‘,
                            user:{
                                username:‘Gary520‘,
                                password:‘5201314‘

                            }
                        }
                    },
                    props:[‘msg‘],
                    template :  "#father-template",
                    components : {
                        "child-component" : {
                            template : "#child-template",
                            props:[‘name‘,‘id‘,‘user‘]
                        }
                    }
                }
            }
        }).$mount("#GaryId");

  同理在父组件中调用数据

    <template id="child-template">
        <div>
            <h2>child component</h2>
            fatherData : <span>{{name}},{{id}},{{user.username}}</span>
        </div>
    </template>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Gary</title>
    </head>
    <body>
        <div id="GaryId">
            <father-component :msg="msg"></father-component>
        </div>
    </body>

<template id="father-template">
        <div>
            <h1>father component</h1>
            myData : <span>{{name}},{{id}},{{user.username}}</span><br />
            fatherDate : <span>msg</span><br />
            <input type="text" v-model="name"/>
            <hr />

            <child-component :name="name" :id="id" :user="user"></child-component>
        </div>
    </template>

    <template id="child-template">
        <div>
            <h2>child component</h2>
            fatherData : <span>{{name}},{{id}},{{user.username}}</span>
        </div>
    </template>

    <script type="text/javascript" src="../js/vue.js" ></script>
    <script type="text/javascript">

        new Vue({
            data : {
                 msg:‘helloVue‘
            },
            components : {
                "father-component" : {
                    data(){
                        return {
                            id:1,
                            name : ‘Gary‘,
                            user:{
                                username:‘Gary520‘,
                                password:‘5201314‘

                            }
                        }
                    },
                    props:[‘msg‘],
                    template :  "#father-template",
                    components : {
                        "child-component" : {
                            template : "#child-template",
                            props:[‘name‘,‘id‘,‘user‘]
                        }
                    }
                }
            }
        }).$mount("#GaryId");

    </script>
</html>

Gary_fatherAndChild.html

 二、Props选项高级选项配置  传送门

  使用Props还可以实现许多功能,如设置默认值、数据校验、设置返回值

new Vue({
            data : {

            },
            components : {
                "father-component" : {
                    data(){
                        return {
                            id : ‘01‘,
                            name : ‘Gary‘,
                            user : {
                                username : ‘Gary520‘,
                                password : ‘5201314‘
                            },
                            age : 20
                        }
                    },
                    props : [‘msg‘],
                    template :  "#father-template",
                    components : {
                        "child-component" : {
                            template : "#child-template",
                            //props : [‘username‘, ‘id‘, ‘user‘]
                            props : {
                                name : {
                                    type : String,
                                    //必须得传值可使用required : true,
                                    default : "Garydef"
                                },
                                id : [Number, String],
                                user : {
                                    type : Object,
                                    default : function(){
                                        return {username : ‘userGary‘, password : ‘pw123‘};
                                    }
                                },
                                age : {
                                    type : Number,
                                    validator : function(value){
                                        return value >= 0;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }).$mount("#GaryId");

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Gary</title>
    </head>
    <body>
        <div id="GaryId">
                <father-component></father-component>
        </div>
    </body>

<template id="father-template">
            <div>
            <h1>father component</h1>
            myData :
            <span>
                {{name}} ,
                {{id}} ,
                {{user.username}} ,
                {{age}}
            </span><br /><hr />
            <child-component  :id="id" :age="age"></child-component>
        </div>
    </template>

    <template id="child-template">
        <div>
            <h2>child component</h2>
            fatherData :
            <span>
                {{name}} ,
                {{id}} ,
                {{user.username}},
                {{age}}
            </span>
        </div>
    </template>

    <script type="text/javascript" src="../js/vue.js" ></script>
    <script type="text/javascript">

            new Vue({
            data : {

            },
            components : {
                "father-component" : {
                    data(){
                        return {
                            id : ‘01‘,
                            name : ‘Gary‘,
                            user : {
                                username : ‘Gary520‘,
                                password : ‘5201314‘
                            },
                            age : 20
                        }
                    },
                    props : [‘msg‘],
                    template :  "#father-template",
                    components : {
                        "child-component" : {
                            template : "#child-template",
                            //props : [‘username‘, ‘id‘, ‘user‘]
                            props : {
                                name : {
                                    type : String,
                                    //必须得传值可使用required : true,
                                    default : "Garydef"
                                },
                                id : [Number, String],
                                user : {
                                    type : Object,
                                    default : function(){
                                        return {username : ‘userGary‘, password : ‘pw123‘};
                                    }
                                },
                                age : {
                                    type : Number,
                                    validator : function(value){
                                        return value >= 0;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }).$mount("#GaryId");

    </script>
</html>

Gary_props.html

原文地址:https://www.cnblogs.com/1138720556Gary/p/10545195.html

时间: 2024-08-03 18:58:23

Vue_(组件通讯)父组件向子组件传值的相关文章

小程序的组件通讯三种方法==子向父传值

小程序的组件通讯三种方法 ============================ ================================ 子向父传值 第一步:小程序子向父传值在父组件定义方法 第二步:小程序子向父传值第二部在使用子组件的标签上在父的wxml文件中把方法传递给子组件 第三步:小程序子向父传值第三步在子组件的js文件中调用this·triggerEvent触发方法同时传递参数给父组件 第四步:第四步在第一步定义好的方法内部通过e·detail来接收子组件传递回来的参数 原

vue父组件异步传递prop到子组件echarts画图问题踩坑总结

效果图: 大致思路:考虑到5张图都是折线图,所以准备用一个子组件承接echarts画图,然后父组件通过prop传递不同数据来展示不同的图 踩坑问题: 1.引入line子组件,画了5个元素,但是只显示一个 原因:id重复 解决方案:prop传递不同id名 2.父组件传递的数据在子组件报错 这里情况比较特殊,我用父组件数据data里面给demo数据的时候,子组件是拿得到数据的,图片正常显示,所以以为可以了,当换成从后台请求的数据后,发现子组件总是报错,data.count is not a func

Vue把父组件的方法传递给子组件调用(评论列表例子)

Vue把父组件的方法传递给子组件调用(评论列表例子) 效果展示: 相关Html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../js/vue-2.4.0.js"></script> <link

vue--父组件向子组件传参--父组件定义v-bind:参数名--子组件接收props----子组件调用父组件的方法(子组件向父组件传参)父组件@事件名称--子组件接收this.$emit

<!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" con

Vue父组件向子组件传递方法(自定义方法)并且子组件向父组件传递数据

<!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" con

layer.open中父页面向子页面传值

1.咱先看图说话 父list.jsp 子operate.jsp实现的代码1 在父页面上完成对子页面的数据渲染 function setData(data) { var lay=layer.open({ type: 2, title: false, //closeBtn: 0, //shade:0,//是否有遮罩效果 area: ['560px', '294px'],//宽,高 //skin: 'layui-layer-nobg', //没有背景色 shadeClose: false, conte

vue的组件通讯 父传子 -- 子传父-- 兄弟组件的传值 vue的组件传值

首先文字简单撸一下 父子传子   -------首先在父组件上绑定一个属性,在子组件里用props接收,可以是数组或者是对象 子传父   ------在父组件升上自定义一个方法,在子组件里通过this.$emit("父组件上的方法名",a)     /-------a------/代表需要传递的参数        兄弟组件通讯   需要创建一个公共的vue 实例, new vue()    在main.js里 书写Vue.prototype .com=new vue()    通过pr

vue.js组件之间通讯--父组件调用子组件的一些方法,子组件暴露一些方法,让父组件调用

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body><div id="app"></div></body><script src="node_modules/vue/

react父子组件通讯-----&gt;下面用到的ref属性调用子组件的方法,可以实现子组件往父组件传递参数,可以通过在父组件的方法中调用子组件的方法,通过返回值来拿到值,也可以在子组件中,对数据处理完后,调用父组件传给子组件的参数或者方法,来实现传参,

<scripttype="text/babel"> var Child =React.createClass({ getInitialState: function() { return {color:"",childMsg:"我是子组件的信息"}; }, changeColor: function(e) { this.setState({color:e.target.getAttribute("data-color&quo