导航

<h1 class="title">{{ title }}</h1>
{{ title }} -> 是插值表达式
https://github.com/janl/mustache.js
npm install mustache --save
import Mustache from 'mustache';
var data = {
title: 'This is my TITLE for MUSTACHE'
};
var html = Mustache.render(
'<h1 class="title">{{ title }}</h1>',
data
);
document.getElementById('app').innerHTML = html;
const App = {
data() {
return {
title: 'This is my TITLE for MUSTACHE',
author: 'Lance',
dateTime: new Date(),
content: 'This is my CONTENT',
}
},
template: `
<div>
<h1 class="title">{{ title }}</h1>
<p>
<span class="author">{{ author }}</span> - {{ dateTime }}
</p>
<p v-bind:title="content">{{ content }}</p>
</div>
`
}
Vue.createApp(App).mount('#app');
import { h } from 'vue';
const App = {
data() {
return {
title: 'This is my TITLE for MUSTACHE',
author: 'Lance',
dateTime: new Date(),
content: 'This is my CONTENT',
}
},
// template: `
// <div>
// <h1 class="title">{{ title }}</h1>
// <p>
// <span class="author">{{ author }}</span> - {{ dateTime }}
// </p>
// <p v-bind:title="content">{{ content }}</p>
// </div>
// `,
render() {
// h函数参数:
// 1. 目标标签,2. 属性集合 3. 子元素
return h(
'div',
{},
[
h(
'h1',
{
class: 'title'
},
this.title
),
h(
'p',
{},
[
h(
'span',
{
class: 'author',
},
this.author
),
` - ${this.dateTime}`
]
),
h(
'p',
{
title: this.title
},
this.content
)
]
)
}
}
Vue.createApp(App).mount('#app');
render 函数获取 template(直接写 template,或者显示声明 render 函数)


目的:解析那些 template 中不符合 html 原生的属性、指令、双大括号等
ast树的样子:
逻辑:识别一段 html 就删除一段,直到最后整个字符串被删除完,最终形成一个完整的AST树
e.g. function fn(a, b) {} 被解析成了下边对象样子


例如一个div元素被解析成下面的虚拟节点

其中:

整个过程:
