组件的注册

组件的注册

  1. 通过实例化Vue构造器函数得到一个Vue实例,这个实例我们称之为’根实例’,它是最大的父级

  2. 这个根实例是以标签的形式存在的,那么我们也称之为’ 根组件 ’

  3. 根实例也是一个组件,但是我们得到只是跟组件

  4. Vue.extend() 它就是对Vue功能的扩展,这个扩展就是组件

  5. Vue是通过 Vue.extend() 来实现【 扩展 】 Vue的功能的,这个功能就是组件

  6. 思考: Vue.extend如何使用?

    • 通过new Vue.extend() 发现和new Vue一样了 排除了
    • 组件就是一个以标签化呈现的东西,所以我应该像标签一样使用
    • 但是无论是 html3 还是 html5 肯定不会同意它随意来个标签的
    • Vue会将组件编译成html结构
    • Vue的这个处理过程,我们称之为 ’ 组件注册 ’

​ 总结:

​ 1. Vue是通过Vue.extend() 来实现组件的

​ 2. Vue的组件的使用时需要注册的

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <!-- 不能这么写 -->
    <!-- <HelloBoy></HelloBoy> -->

    <!-- <hello-boy></hello-boy> -->

    <Hello></Hello>
    <hello></hello>

  </div>


  <div id="root">
    <Hello></Hello>
  </div>
</body>
<script src="../../lib/vue.js"></script>
<script>
  /* 
    全局注册
      1. 格式: Vue.component()
  */

  // const Hello = Vue.extend( options ) 我们之所学的选项这里都可以用
  const Hello = Vue.extend({
    template: '<div> Hello 组件 </div>'
  })
  // Vue.component( 组件的名称,组件的配置 )
  // Vue.component( 'HelloBoy', Hello )
  // Vue.component( 'hello-boy', Hello )
  Vue.component( 'Hello', Hello )
  
  new Vue({
    el: '#app'
  })


  new Vue({
    el: '#root'
  })
</script>
</html>

简写

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <!-- 不能这么写 -->
    <!-- <HelloBoy></HelloBoy> -->

    <!-- <hello-boy></hello-boy> -->

    <Hello></Hello>
    <hello></hello>

  </div>


  <div id="root">
    <Hello></Hello>
  </div>
</body>
<script src="../../lib/vue.js"></script>
<script>
  /* 
    全局注册
      1. 格式: Vue.component()
  */

  // vue组件注册的简写
  Vue.component( 'Hello', {
    template: '<div> Hello 组件 </div>'
  })
  
  new Vue({
    el: '#app'
  })


  new Vue({
    el: '#root'
  })
</script>
</html>

组件的局部注册

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <!-- 不能这么写 -->
    <!-- <HelloBoy></HelloBoy> -->

    <!-- <hello-boy></hello-boy> -->

    <Hello></Hello>
    <hello></hello>

  </div>


  <div id="root">
    <Hello></Hello>
  </div>
</body>
<script src="../../lib/vue.js"></script>
<script>


  /* 
    局部注册使用  components选线来完成
    局部注册只在当前注册的实例范围内有效
  */
  new Vue({
    el: '#app',
    components: { //局部注册组件的选项
      // 组件的名称: 组件的选项
      'Hello': {
        template: '<div> Hello 这里是局部注册 </div>'
      }
    }
  })


  new Vue({
    el: '#root'
  })
</script>
</html>

组件的使用规则

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="app">

    <table border="1"> 
      <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
      </tr>
      <tr is = "Hello"></tr>
    </table>

  </div>
</body>
<script src="../../lib/vue.js"></script>
<script>
  /* 
    业务: 给上面的表格增加一个行,这一行用一个组件表示
    

    通过以上案例发现: 组件在父子级是有直接关系的标签中是不能直接解析的,会出问题

    直接父子级关系的标签
      ul li 
      ol li
      table tr  td
      dl dd dt
      select 
      
    如何解决这个问题?

      我们使用is属性来解决

  */

  Vue.component('Hello',{
    template: ` <tr>
                  <td>4</td>
                  <td>5</td>
                  <td>6</td>
                </tr>`
  })

  new Vue({
    el: '#app'
  })
</script>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值