【Vue实战】组件基础

组件基础

1、组件局部注册的用法
<body>
    <div id="app">
        <!--3.my-component只能在#app下使用-->
        <my-component></my-component>
    </div>
    <script>
    //1.创建一个组件构造器
    var myComponent=Vue.extend({
    
        template:'<div>this is my first component!</div>'
    })
    var vm=new Vue({
    
        template:'<div>this is my first component!</div>'
    })
    var vm=new Vue({
    
        el:'#app',
        components:{
    
            //2.将myComponent组件注册到vue实例下
            'my-component':myComponent
        }
    });
    </script>
</body>
2. 验证组件全局注册的用法
<body>
    <div id="app">
        <!--3.#app是vue实例挂载的元素,应该在挂载元素范围内使用组件-->
        <my-component></my-component>
    </div>
    <div id="app2">
        <my-component></my-component>
    </div>
    <my-component></my-component>
    <script>
    //1.创建一个组件构造器
    var myComponent=Vue.extend({
    
        template:'<div>This is my first component!</div>'
    })
    //2.注册组件,并指定组件的标签,组件的HTML标签为<my-component>
    Vue.component('my-component',myComponent)
    var vm=new Vue({
    
        el:'#app'
    });

    var vm1=new Vue({
    
        el:'#app2'
    })
    </script>
</body>
3. 验证通过组件注册语法糖简化全局注册步骤
<body>
    <div id="app1">
            <my-component1></my-component1>
    </div>
    <div id="app2">
            <my-component2></my-component2>
            <my-component3></my-component3>
            <my-component1></my-component1>
    </div>

    <script>
    //全局注册,my-component1是标签名称
    Vue.component('my-component1',{
    
        template:'<div>this is the first component!</div>'
    })
    var vm1=new Vue({
    
        el:'#app1'
    })
    var vm2=new Vue({
    
        el:'#app2',
        components:{
    
            //局部注册,my-component2是标签名称
            'my-component2':{
    
                template:'<div>this is the second component!</div>'
            },
            //局部注册,my-component3是标签名称
            'my-component3':{
    
                template:'<div>this is the third component!</div>'
            }
        }
    })
    </script>
</body>
4. 1验证使用script或template标签将定义在JavaScript中的HTML模板分离出来
<body>
    <div id="app">
        <my-component></my-component>
    </div>
    <script type
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值