Vue简单实现原理详解

2020-06-16 06:55:36易采站长站整理

const childNodes = el.childNodes;
Array.from(childNodes).forEach((node) => {
if(this.isElement(node)){
// 如果是元素
console.log("编译元素"+node.nodeName)
// 拿到元素上所有的执行,伪数组
const nodeAttrs = node.attributes;
Array.from(nodeAttrs).forEach((attr) => {
// 属性名
const attrName = attr.name;
// 属性值
const exp = attr.value;
// 如果是指令
if(this.isDirective(attrName)){
// q-text
// 获取指令后面的内容
const dir = attrName.substring(2);
// 执行更新
this[dir] && this[dir](node,this.$vm,exp);
}
// 如果是事件
if(this.isEvent(attrName)){
// 事件处理
let dir = attrName.substring(1); // @
this.eventHandler(node,this.$vm,exp,dir);
}
})
}else if(this.isInterpolation(node)){
// 如果是插值文本
this.compileText(node);
console.log("编译文本"+node.textContent)
}
// 递归子元素,解决元素嵌套问题
if(node.childNodes && node.childNodes.length){
this.compile(node);
}
})
}
// 是否为节点
isElement(node){
return node.nodeType === 1;
}
// 是否为插值文本
isInterpolation(node){
return node.nodeType === 3 && /{{(.*)}}/.test(node.textContent);
}
// 是否为指令
isDirective(attr){
return attr.indexOf("q-") == 0;
}
// 是否为事件
isEvent(attr){
return attr.indexOf("@") == 0;
}

// v-text
text(node,vm,exp){
this.update( node, vm, exp, "text");
}
textUpdater(node,value){
node.textContent = value;
}

// 双向绑定
// v-model
model(node,vm,exp){
// 指定input的value属性,模型到视图的绑定
this.update(node,vm,exp,"model");
// 试图对模型的响应
node.addEventListener('input',(e) => {
vm[exp] = e.target.value;
})
}
modelUpdater(node,value){
node.value = value;
}

// v-html
html(node,vm,exp){
this.update(node,vm,exp,"html")
}
htmlUpdater(node,value){
node.innerHTML = value;
}

// 更新插值文本
compileText(node){
let key = RegExp.$1;
this.update( node, this.$vm, key, "text");
}
// 事件处理器
eventHandler(node,vm,exp,dir){
let fn = vm.$options.methods && vm.$options.methods[exp];
if(dir && fn){
node.addEventListener(dir,fn.bind(vm));
}
}