Code Monkey home page Code Monkey logo

tiny-vue's Introduction

tiny-vue

项目结构

  • runtime-core
    • render-api
      • index (DOM 操作)
    • component (创建组件)
    • createApp (创建应用)
    • createVNode (创建虚拟节点)
    • h (渲染函数)
    • index (入口)
    • renderer (渲染器)
  • shared
    • index (入口)
    • shapeFlags (节点类型定义)

流程

  • 调用 createApp 函数
    1. 创建 app 对象。包含 component(节点)、container(容器)、mount(挂载函数)
  • 调用 app 对象中的 mount 函数
    1. 根据传入的 container 和先有的 component 创建 VNode
      • VNode:
        1. el - DOM 元素
        2. component - 组件
        3. type - 节点
        4. props - 属性
        5. children - 子组件
        6. shapeFlag - 节点类型
    2. 调用渲染器 renderer 中的 render 函数
  • render 函数
    • patch 函数
      • 文字节点
      • 注释节点
      • 静态节点
      • fragment 节点
      • 标签节点
        • processElement 函数,未挂载调用 mountElement, 挂载调用 updateElement
          • mountElement
            1. 因为是标签,则直接创建 DOM 元素
            2. 判断子组件类型,文本类型 则直接将文本设置到 DOM 上,多节点则调用 mountChildren
              • mountChildren 函数中 循环遍历每一个子节点,然后调用 patch 函数
            3. 处理 props。调用 hostPatchProp
            4. beforeMount hook
            5. 当前 DOM 插入到 容器
            6. mounted hook
          • updateElement
            1. 设置 n2.el = n1.el
            2. patchProps 函数比较新旧节点的属性
              • 循环新节点的 props,prop 不一致则更新
              • 查找旧节点中有,而新节点中没有的,则移除
              • 新节点中有,而旧节点中没有的,则新增
            3. patchChildren 函数比较新旧节点的子节点
              • 判断当前节点的类型,是文本子节点,则比较新旧子节点,然后更新
      • 组件节点
        • processComponent 函数,未挂载调用 mountComponent,挂载调用 updateComponent
          • mountComponent
            1. 创建组件实例
            2. setupComponent 函数
              1. initProps
              2. initSlots
              3. setupStatefulComponent
                1. 调用实例上的 setup 函数
                2. 处理 setup 的回调值,调用 handleSetupResult 函数
                  1. 判断是 function 还是 object
                  2. 调用 finishComponentSetup
            3. setupRenderEffect 函数
              1. 调用 effect 函数,传一个方法入参
          • updateComponent

bug

  • example/apiinject/App.js 注释代码无法实现功能。

tiny-vue's People

Watchers

Fang BoWen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.