bison1994 / two-way-data-binding Goto Github PK
View Code? Open in Web Editor NEWA simple interpretation of how Vue.js realizes two-way data-binding.
A simple interpretation of how Vue.js realizes two-way data-binding.
html代码中44行 这句是不是少了个”=“
vm.text = 1;
text能变,input没有变化
你好
我在控制台输入 vm.data.text = 'hello'; 发现没有触发对象属性访问器中的set方法,进而视图没有发生变化
比如data新增text2:‘newmsg’,在初始化渲染的时候,{{text}} {{text2}}连在一起,这时候代码认为这是连在一起的文本节点,请问该如何区分,分别渲染text和text2
您好,首先感谢您的分享,让我学到很多。
不过有一点还没有看明白,就是index.html文件里面第68行的那个给node.value赋值是为啥?
我删除掉以后发现也是OK的,仔细看了逻辑,也没有发现哪里需要的地方,如果有空能说下就太感谢🙏了
实例42行报错: flag.append(child);
应改为:flag.appendChild(child);
如果把compile做一下递归,就可以了
function compile (node, vm) {
var reg = /\{\{(.*)\}\}/;
//这里加一个递归
if(node.childNodes && node.childNodes.length>0){
for(var i=0,len =node.childNodes.length;i<len;i++ ){
compile(node.childNodes[i],vm);
}
}
// 节点类型为元素
if (node.nodeType === 1) {
var attr = node.attributes;
// 解析属性
for (var i = 0; i < attr.length; i++) {
if (attr[i].nodeName == 'v-model') {
var name = attr[i].nodeValue; // 获取v-model绑定的属性名
node.addEventListener('keyup', function (e) {
// 给相应的data属性赋值,进而触发该属性的set方法
vm[name] = e.target.value;
});
node.value = vm[name]; // 将data的值赋给该node
node.removeAttribute('v-model');
}
};
new Watcher(vm, node, name, 'input');
}
// 节点类型为text
if (node.nodeType === 3) {
if (reg.test(node.nodeValue)) {
var name = RegExp.$1; // 获取匹配到的字符串
name = name.trim();
new Watcher(vm, node, name, 'text');
}
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.