Vue父子、父子孙组件嵌套

单个组件的语法:

创建构造器
var MyComponent = Vue.extend({
        template: '<div>A custom component!</div>'
    });
    注册组件
Vue.component('my-component', MyComponent);

父子组件的写法:

 var child = Vue.extend({
        template:"<h1>我是孩子</h1>"
    });
      Vue.component("parent",{
          template:"<h1>我是父亲<child1></child1></h1>",
          components:{
              "child1":child
          }
      })

父子嵌套实例:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="vue.js" type="text/javascript"></script>
</head>
<body>
<div id="box">
    <parent></parent>
</div>
<script>
    window.onload=function(){
        new Vue({
            el:"#box"
        });
    };
    var child = Vue.extend({
        template:"<h1>我是孩子</h1>"
    });
      Vue.component("parent",{
          template:"<h1>我是父亲<child1></child1></h1>",
          components:{
              "child1":child
          }
      })
</script>

</body>
</html>

父子孙组件嵌套:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="vue.js" type="text/javascript"></script>
</head>
<body>
<div id="box">
    <parent></parent>
</div>
<script>
    window.onload=function(){
        new Vue({
            el:"#box"
        });
    };
    var child = Vue.extend({
        template:"<h1>我是孙子</h1>"
    });
    var son = Vue.extend({
        template:"<h1>我是儿子<child1></child1></h1>",
        components:{
            "child1":child
        }
    });
    Vue.component("parent",{
        template:"<h1>我是父亲<son1></son1></h1>",
        components:{
            "son1":son
        }
    })
</script>

</body>
</html>

父子组件另一种写法:

var vm=new Vue({
        el:"#box",
        data:{
            a:"aaa"
        },
        components:{
            "aaa":{
                template:"<h2>我是aaa</h2><bbb></bbb>",
                components:{
                    "bbb":{
                        template:"<h2>我是bbb</h2>",
                    }
                }
            }
        }
    })
<div id="box">
   <aaa></aaa>
</div>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值