Vue-父子组件传参

<name-item></name-item> //1.单独声明注册组件
<div id="myApp">
    <my-weather></my-weather> //2.Vue实例中的局部组件
    
    <table>
        <tr>
            <td>編號</td>
            <td>遊戲名稱</td>
        </tr>
        <tr is="my-row"></tr> //3.is属性加载自定义表行组件
    </table>
    
    <div>今天的天氣是<today-weather/></div> //4.组件数据函数
    
    <compute-result :num=5></compute-result> //5.向组件传参num
</div>
<script>
    Vue.component('name-item',{   //1.单独声明注册组件
        template: '<li>连翘</li>'
    })
    Vue.component('my-row',{    //3.表行组件
        template: '<tr><td>(1)</td><td>英雄联盟</td>'
    })
    Vue.component('today-weather',{  //4.组件数据函数
        template: '<strong>{{todayWeather}}</strong>',
        data: function(){  //是函数!!!!!
            return {   //返回数据变量
                todayWeather: '小雨'
            }
        }
    })
    Vue.component('compute-result',{  //5.向组件传参
        props: {
            num: {         //组件接收到的参数num
                type: Number,
                required: true //必要参数
            }
        }, 
        template: '<div>{{num}}*2={{computeResult}}</div>', //组件模板
        computed: {
            computeResult: function(){
                return this.num*2
            }
        }
    })
    var myApp = new Vue({
        el: "#myApp",
        data: {},
        components: {   //2.该方法生成的组件只能在Vue实例myApp中使用。
            'my-weather': WeatherComponent //组件名:模板对象
        }
    })
</script>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值