vue组件的创建和注册

基本步骤

Vue.js的组件的使用有3个步骤:创建组件构造器、注册组件和使用组件。

Vue.extend()方法创建组件构造器

Vue.component()方法注册组件

Vue实例的作用范围内使用组件

<!DOCTYPE html><html>

    <body>

        <div id="app">

            <!-- 3. #app是Vue实例挂载的元素,应该在挂载元素范围内使用组件-->

            <my-component></my-component>

        </div>

    </body>

    <script src="js/vue.js"></script>

    <script>

    

        // 1.创建一个组件构造器

        var myComponent = Vue.extend({

            template: '<div>This is my first component!</div>'

        })

        

        // 2.注册组件,并指定组件的标签,组件的HTML标签为<my-component>

        Vue.component('my-component', myComponent)

        new Vue({

            el: '#app'

        });

    </script></html>

 

 

上面的示例可以改为局部注册的方式:

<!DOCTYPE html><html>

    <body>

        <div id="app">

            <!-- 3. my-component只能在#app下使用-->

            <my-component></my-component>

        </div>

    </body>

    <script src="js/vue.js"></script>

    <script>

        // 1.创建一个组件构造器

        var myComponent = Vue.extend({

            template: '<div>This is my first component!</div>'

        })

        

        new Vue({

            el: &

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值