组件的VueComponent

VueComponent

  <div class="root">
    <shool></shool>
    <hello></hello>
  </div>
  <script>
    const student = Vue.extend({
      template:`<h1>高高</h1>`,
    })
    const shool = Vue.extend({
      template:`<div>
        <h1>理工</h1>
        <student></student>
      </div>`,
      components:{
        student
      }
    })
    const hello = Vue.extend({
      template:`<h1>good</h1>` 
    })
    new Vue({
      el:'.root',
      components:{
        shool,
        hello
      }
    })
    // console.log("===>>>>",shool)
    // console.log(shool == hello)
  </script>
shool
//ƒ VueComponent (options) {
//        this._init(options);
//      }
student
//ƒ VueComponent (options) {
//        this._init(options);
//      }
hello
//ƒ VueComponent (options) {
//        this._init(options);
//      }
  • 从上面看,可以看出每个组件都是一个构造函数
hello === shool
//false
  • 他们不是同一个构造函数,都是新的构造函数,每次都返回一个新的VueComponent


    vuecomponent.PNG
  • 这是新的VueComponent由Vue.extend产生的

  • 只要写 <shool></shool>这样的组件标签,vue会自动帮new VueComponent 成一个实例对象

  • new Vue的this指向的是Vue实例对象

  <div class="root">
    <shool></shool>
    <hello></hello>
  </div>
  <script>
    const student = Vue.extend({
      template:`<h1>高高</h1>`,
    })
    const shool = Vue.extend({
      template:`<div>
        <h1>理工</h1>
        <student></student>
        <button @click="shoolName">看(shoolName)VueComponent</button>
      </div>`,
      components:{
        student
      },
      methods:{
        shoolName(){
          console.log("------shool-------",this)
        }   
      }
    })
    const hello = Vue.extend({
      template:`<div><h1>good</h1><button @click="shoolName">看(hello)VueComponent</button></div>` ,
      methods:{
        shoolName(){
          console.log("-------hello------",this)
        }   
      }
    })
    const vm = new Vue({
      el:'.root',
      components:{
        shool,
        hello
      }
    })
    // console.log("===>>>>",shool)
    // console.log(shool == hello)
  </script>
看(shoolName)VueComponent.PNG
  • 看shool组件的this,点击shool按钮
  • 组件的this指向VueComponent实现对象
  • 可以看到属性$children: Array(1),里面有个数组,数组就是那shool嵌套的student属性

6.内置关系
  1.一个重要的内置关系:VueComponent.prototype.proto === Vue.prototype
2.为什么要有这个关系:让组件实例对象(vc)可以访问到 Vue原型上的属性、方法。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 作者:lihongxun945github.com/lihongxun945/myblog/issues/23 这...
    grain先森阅读 913评论 0 4
  • 一、概念介绍 Vue.js和React.js分别是目前国内和国外最火的前端框架,框架跟类库/插件不同,框架是一套完...
    刘远舟阅读 1,087评论 0 0
  • 初识Vue 1.想让Vue工作,就必须创建一个Vue实例,且要传入一个配置对象;2.root容器里的代码依然符合h...
    ARGM10阅读 480评论 0 0
  • 官网上关于组件继承分为两大类,全局组件和局部组件。无论哪种方式,最核心的是创建组件,然后根据场景不同注册组件。 有...
    娜姐聊前端阅读 1,833评论 1 5
  • [TOC] Vue 学习笔记 Vue 源码解析 - 主线流程 Vue 源码解析 - 模板编译 Vue 源码解析 -...
    Whyn阅读 740评论 0 0