Skip to content
On this page

什么是编译时?

在上一小节中,我们明确了,如果只靠 进行时 ,那么是没有办法通过 HTML标签结构的方式 来进行渲染解析的。

那么想要实现这一点,我们就需要借助另外一个东西,也就是本节的标题: 编译时

Vue 中的编译时,更准确的说法应该是 编译器 的意思。它的代码主要存在于 compiler-core 模块下。

compiler-core

我们来看如下代码:

html
<body>
    <div id="app"></div>
</body>
<script>
    const { compile, createApp } = Vue
    // 创建一个html结构
    const html = `
        <div class="test">hello compiler</div>
    `
    // 利用compile函数生成render函数
    const renderFn = compile(html)
    // console.log(renderFn)
    // 创建实例
    const app = createApp({
        // 利用render函数渲染
        render:renderFn
    })
    // 挂载
    app.mount('#app')
</script>

代码例子依旧在老地方 以后不在赘述

对于编译器而言,它的主要作用就是: 把 template 中的 html 编译成 render 函数。然后再利用 运行时 通过 render 挂载对应的 DOM。

总结一下: 编译时可以把 html 结构的节点,编译成 render 函数

Released under the MIT License.