前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >组件注册

组件注册

作者头像
梨涡浅笑
发布2022-01-06 14:25:45
1.2K0
发布2022-01-06 14:25:45
举报
文章被收录于专栏:全栈自学笔记全栈自学笔记

1.组件注册

1.1 全局注册
  • Vue.component('组件名称', { }) 第1个参数是标签名称,第2个参数是一个选项对象
  • 全局组件注册后,任何vue实例都可以用
组件基础使用
代码语言:javascript
复制
<body>
    <div id="app">
        <!-- 2、 组件使用 组件名称 是以HTML标签的形式使用  -->  
        <button-counter></button-counter>
    </div>
    <script src="vue.js"></script>
    <script>
         //   注册组件 
         // 1、 button-counter 就是组件中自定义的标签名
        Vue.component('button-counter', {
            data: function () {
                return {
                    count: 0
                }
            },
            // template: '<button @click="count++">点击了{{count}}次</button>',
            template: '<button @click="handel">点击了{{count}}次</button>',
            methods: {
                handel: function () {
                    this.count += 2;
                }
            }
        })
        // 创建根实例
        var vm = new Vue({
            el: '#app',
            data: {
​
            },
            methods: {
​
            }
        })
    </script>
</body>
组件注意事项
  • 组件参数的data值必须是函数同时这个函数要求返回一个对象 分析函数与普通对象的对比
  • 组件模板必须是单个根元素 分析演示实际的效果
  • 组件模板的内容可以是模板字符串 模板字符串需要浏览器提供支持(ES6语法)
  • 组件命名方式
代码语言:javascript
复制
 <div id="app">
     <!-- 
        4、  组件可以重复使用多次 
          因为data中返回的是一个对象所以每个组件中的数据是私有的
          即每个实例可以维护一份被返回对象的独立的拷贝   
    --> 
    <button-counter></button-counter>
    <button-counter></button-counter>
    <button-counter></button-counter>
      <!-- 8、必须使用短横线的方式使用组件 -->
     <hello-world></hello-world>
  </div>
​
<script type="text/javascript">
    //5  如果使用驼峰式命名组件,那么在使用组件的时候,只能在字符串模板中用驼峰的方式使用组件,
    // 7、但是在普通的标签模板中,必须使用短横线的方式使用组件
     Vue.component('HelloWorld', {
      data: function(){
        return {
          msg: 'HelloWorld'
        }
      },
      template: '<div>{{msg}}</div>'
    });
    
    
    
    Vue.component('button-counter', {
      // 1、组件参数的data值必须是函数 
      // 同时这个函数要求返回一个对象  
      data: function(){
        return {
          count: 0
        }
      },
      //  2、组件模板必须是单个根元素
      //  3、组件模板的内容可以是模板字符串  
      template: `
        <div>
          <button @click="handle">点击了{{count}}次</button>
          <button>测试123</button>
            #  6 在字符串模板中可以使用驼峰的方式使用组件   
           <HelloWorld></HelloWorld>
        </div>
      `,
      methods: {
        handle: function(){
          this.count += 2;
        }
      }
    })
    var vm = new Vue({
      el: '#app',
      data: {
        
      }
    });
  </script>
1.2局部注册
  • 只能在当前注册它的vue实例中使用
代码语言:javascript
复制
<body>
    <div id="app">
        <hello-world></hello-world>
        <hello-tom></hello-tom>
        <hello-jrreny></hello-jrreny>
        <text-com></text-com>
    </div>
    <script src="vue.js"></script>
    <script>
        // 定义组件的模板
        Vue.component('text-com', {
            template: '<div>Text<hello-tom></hello-tom></div>' //局部组件只能在注册他的父组件中使用
        })
        var HelloWorld = {
            data: function () {
                return {
                    msg: 'HelloWorld'
                }
            },
            template: '<div>{{msg}}</div>'
        };
        var HelloTom = {
            data: function () {
                return {
                    msg: 'HelloTom'
                }
            },
            template: '<div>{{msg}}</div>'
        };
        var HelloJrreny = {
            data: function () {
                return {
                    msg: 'HelloJrreny'
                }
            },
            template: '<div>{{msg}}</div>'
        };
        var vm = new Vue({
            el: '#app',
            data: {
​
            },
             // 局部组件注册
            components: {
                //将只在父模板可用  一定要在实例上注册了才能在html文件中使用
                'hello-world': HelloWorld,
                'hello-tom': HelloTom,
                'hello-jrreny': HelloJrreny
            },
            methods: {
​
            }
        })
    </script>
</body>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-12-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.组件注册
    • 1.1 全局注册
      • 组件基础使用
      • 组件注意事项
    • 1.2局部注册
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档