父子组件相互传参

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>父子组件传参组件</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
    <div id="itapp">
        <!-- 父组件通过props向下传递数据给子组件 -->
        <!-- <my-world :message='msg' :name="name" v-on:e-world='getData()'></my-world> -->
        <my-world :message='msg' :name="name" @e-world='getData'></my-world>
        <h1>我是父组件</h1>
        <!-- {{testName}} -->
        <h2>访问自己的数据:{{msg}}</h2>
        <h2>访问到子组件的数据:{{testkk}}</h2>
    </div>

    
    <template id="world">
        <div>
            <h1>我是world组件</h1>
           <h2>访问我自己的数据sex:{{sex}}</h2>
           <h2>访问父组件中的数据: 
               {{message}} 
               {{name}}
               {{age}}
               {{user.username}}
            </h2>
            <button type="button" @click="send"> 将子组件的数据向上传递给父组件</button>
        </div>
    </template>
    <script>
        var childWorld = {
            // props:['message','name','age','user'],
            props:{
                //也可以为对象,允许配置高级设置,类型判断,数据检测,设置默认值
                message:String,
                name:{
                    type:String,
                    required:true
                },
                age:{
                    type:Number,
                    default:18,
                    validator:function(value){
                        return value>0
                    }
                },
                user:{
                    type:Object,
                    default:function(){
                        //对象或者数组的默认值必须使用函数进行返回
                       return {
                        id:100,
                        username:'秋香'
                      }
                    }
                }
            },
            data(){
                return {
                    sex:'male',
                    height:'190',
                    testName:'测试',
                }
            },
            methods:{
                send(){
                    this.$emit('e-world',{
                        testName:this.testName,
                        sex:this.sex
                    })
                }
            },
            template:'#world'
        };


        var vm=new Vue({ //这里的vm也是一个组件,称为根组件Root ,父组件
			el:'#itapp',
			data(){
                return {
					msg:'黑客',
					name:'tom',
					age:23,
					user:{id:9527,username:'唐伯虎'},
					sex:'',
					height:'',
                    testkk:{}
				}
            },
            components:{
                //子组件
                'my-world':childWorld
            },
            methods:{
                getData(data){
                    this.testkk = data;
                    console.log('sss');
                }
            }
		});	

    </script>
</body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值