博客
关于我
实现一个简易Vue(三)Compiler
阅读量:353 次
发布时间:2019-03-04

本文共 1959 字,大约阅读时间需要 6 分钟。

3. Compiler

  • 功能
    • 负责编译模板,解析指令及插值表达式
    • 负责页面的首次渲染
    • 在数据变化后重新渲染视图
  • 代码
class Compiler {     constructor(vm) {       this.el = vm.$el;       this.vm = vm;       this.compile(this.el);     }     compile(el) {       let childNodes = el.childNodes;       Array.from(childNodes).forEach(node => {         if (this.isTextNode(node)) {           this.compileText(node);         } else if (this.isElementNode(node)) {           this.compileElement(node);         }         if (node.childNodes && node.childNodes.length) {           this.compile(node);         }       });     }     compileElement(node) {       Array.from(node.attributes).forEach(attr => {         if (this.isDirective(attr.name)) {           const attrName = attr.name.substring(2);           this.update(node, attr.value, attrName);         }       });     }     update(node, key, attrName) {       const updateFn = this[attrName + 'Updater'];       if (updateFn && updateFn.call(this, node, this.vm[key], key)) {         return true;       }     }     textUpdater(node, value, key) {       node.textContent = value;       new Watcher(this.vm, key, (newValue) => {         node.textContent = newValue;       });     }     modelUpdater(node, value, key) {       node.value = value;       new Watcher(this.vm, key, (newValue) => {         node.value = newValue;       });       node.addEventListener('input', () => {         this.vm[key] = node.value;       });     }     compileText(node) {       const reg = /\{\{(.+?)\}\}/;       const value = node.textContent;       if (reg.test(value)) {         const key = RegExp.$1.trim();         node.textContent = value.replace(reg, this.vm[key]);         new Watcher(this.vm, key, (newValue) => {           node.textContent = newValue;         });       }     }     isDirective(attrName) {       return attrName.startsWith('v-');     }     isTextNode(node) {       return node.nodeType === 3;     }     isElementNode(node) {       return node.nodeType === 1;     } }

转载地址:http://drir.baihongyu.com/

你可能感兴趣的文章
Objective-C实现linear algebra线性代数算法(附完整源码)
查看>>
Objective-C实现linear congruential generator线性同余发生器算法(附完整源码)
查看>>
Objective-C实现linear discriminant analysis线性判别分析算法(附完整源码)
查看>>
Objective-C实现linear regression线性回归算法(附完整源码)
查看>>
Objective-C实现linear search线性搜索算法(附完整源码)
查看>>
Objective-C实现Linear search线性搜索算法(附完整源码)
查看>>
Objective-C实现LinearSieve线性素数筛选算法 (附完整源码)
查看>>
Objective-C实现LinkedListNode链表节点类算法(附完整源码)
查看>>
Objective-C实现LinkedList链表算法(附完整源码)
查看>>
Objective-C实现local weighted learning局部加权学习算法(附完整源码)
查看>>
Objective-C实现logistic regression逻辑回归算法(附完整源码)
查看>>
Objective-C实现logistic sigmoid函数(附完整源码)
查看>>
Objective-C实现longest Common Substring最长公共子串算法(附完整源码)
查看>>
Objective-C实现longest increasing subsequence最长递增子序列算法(附完整源码)
查看>>
Objective-C实现longestCommonSubsequence最长公共子序列算法(附完整源码)
查看>>
Objective-C实现LongestIncreasingSubsequence最长递增子序列算法(附完整源码)
查看>>
Objective-C实现lorenz transformation 洛伦兹变换算法(附完整源码)
查看>>
Objective-C实现Lower-Upper Decomposition上下分解算法(附完整源码)
查看>>
Objective-C实现LowerCaseConversion小写转换算法(附完整源码)
查看>>
Objective-C实现lowest common ancestor最低共同祖先算法(附完整源码)
查看>>