一、模版解析的分析
- 模版解析:对模版进行拆解分析。
- 模版解析的实现过程:
- 将
el
的所有子节点取出, 添加到一个新建的文档 fragment
对象中
- 对
fragment
中的所有层次子节点递归进行编译解析处理
- 对大括号表达式文本节点进行解析
- 对元素节点的指令属性进行解析
- 将解析后的
fragment
添加到 el
中显示
- 模版解析的关键过程:
function Compile(el, vm) {
this.$vm = vm;
this.$el = this.isElementNode(el) ? el : document.querySelector(el);
if (this.$el) {
this.$fragment = this.node2Fragment(this.$el);
this.init();
this.$el.appendChild(this.$fragment);
}
}
- 模版解析在挂载原型
prototype
上,通过document.createDocumentFragment()
创建fragment
对象,核心代码如下:
node2Fragment: function (el) {
var fragment = document.createDocumentFragment(),
child;
while (child = el.firstChild) {
fragment.appendChild(child);
}
return fragment;
}
- 关于
vue
的源码分析,我在github
上建立了一个项目,项目地址如下https://github.com/jiuchengTk279/vueSourceCode.git
,欢迎大家访问下载,也希望可以多给予一些建议交流。