Vue设计原理解读之初始化挂载流程(二)

本文目的是为探究vue初始化时的挂载流程,主要包括如下核心问题:

  • 渲染器的创建
  • vnode的创建
  • vnode的渲染

环境准备

  1. 克隆vuejs/core到本地:git clone https://github.com/vuejs/core.git
  2. pnpm i安装依赖==> pnpm run dev-esm打一个runtime的包==>打出来的包在packages/vue/dist/vue.runtime.esm-bundler.js,并且生成了sourceMap便于调试
  3. 创建test文件,文件位置如下:
html 复制代码
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
      </head>
  <body>
    <div id="root"></div>
    <script type="module">
      import { h, ref, createApp } from '../../dist/vue.runtime.esm-bundler.js'

      const count = ref(0)

      const HelloWorld = {
        name: 'Child',
        render() {
          return h('div', `我是子组件: count: ${count.value}`)
        },
      }

      const App = {
        name: 'App',
        render() {
          return h('div', { tId: 1 }, [h('p', {}, '我是父组件'), h(HelloWorld)])
        },
      }

      createApp(App).mount(document.querySelector('#root'))
    </script>
  </body>
</html>
  1. 创建vscode的debugger文件,可进行本地调试
launch.json 复制代码
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch Chrome",
      "request": "launch",
      "type": "chrome",
      "url": "http://127.0.0.1:5501/packages/vue/examples/exam/index.html",
      "webRoot": "${workspaceFolder}"
    }
  ]
}

上述文件中的端口自行修改 ,我们打个断点,然后按F5就可以打开调试页面了。:

源码看起来比较复杂,我是通过画思维导图来学习整体流程的。

相关推荐
崔庆才丨静觅3 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby60613 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了3 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅4 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅4 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅4 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment4 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅5 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊5 小时前
jwt介绍
前端
爱敲代码的小鱼5 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax