
vue的双向绑定原理及实现
??????? ??-????? ???????
发布日期:2021-05-10 03:19:05
浏览次数:26
分类:精选文章
本文共 2903 字,大约阅读时间需要 9 分钟。
Vue?????????
??
?????Vue?????????????????????????????????????????????????????????????????????????????????????????????????????
??????
Vue??????????????-???????????????????????????????????????????????????????????????????????
???????
Vue??Object.defineProperty
?????????????????get
?set
???????????????????????????????????
??-????
?????????????-?????????Dep
???????????????????notify
??????????????????????????????????????
????
1. ????Observer
Observer???????????????????????????????get
?set
???????????????
function defineReactive(data, key, val) { observe(val); // ????????? var dep = new Dep(); Object.defineProperty(data, key, { enumerable: true, configurable: true, get: function() { if (Dep.target) { dep.addSub(Dep.target); } return val; }, set: function(newVal) { if (val === newVal) return; val = newVal; console.log('??' + key + '?????????????' + newVal.toString() + '?'); dep.notify(); } });}
2. ????Watcher
Watcher???????????????????????????get
?????????????????
function Watcher(vm, exp, cb) { this.vm = vm; this.exp = exp; this.cb = cb; this.value = this.get();}Watcher.prototype = { update: function() { this.run(); }, run: function() { var value = this.vm.data[this.exp]; var oldVal = this.value; if (value !== oldVal) { this.value = value; this.cb.call(this.vm, value, oldVal); } }, get: function() { Dep.target = this; var value = this.vm.data[this.exp]; Dep.target = null; return value; }};
3. ????Compile
Compile????????????????????JavaScript???????????????????????
function compile(node) { var nodeAttrs = node.attributes; Array.prototype.forEach.call(nodeAttrs, function(attr) { var attrName = attr.name; if (isDirective(attrName)) { var exp = attr.value; var dir = attrName.substring(2); if (isEventDirective(dir)) { compileEvent(node, vm, exp, dir); } else { compileModel(node, vm, exp, dir); } node.removeAttribute(attrName); } });}
??????
??????????????????????
Observer
???????????????Dep
???????????????????????Compile
???????????JavaScript???????????????
????????????SelfVue
??
function SelfVue(options) { var self = this; this.vm = this; this.data = options.data; Object.keys(this.data).forEach(function(key) { self.proxyKeys(key); }); observe(this.data); new Compile(options.el, this); options.mounted.call(this);}
????
?????????????????
{title}
{name}
????????????
- ???????????????
- ????????
v-model
????????????? - ???????????????????????
??
???Vue???????????????????????????????????????????????????????Vue????????????
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年04月22日 21时50分57秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
LintCode Python 简单级题目 8.旋转字符串
2023-01-31
LintCode 第一题fizz buzz
2023-01-31
lintcode-418-整数转罗马数字
2023-01-31
LintCode: Longest Common Substring
2023-01-31
Lintcode: Nuts & Bolts Problem
2023-01-31
Lintcode: O(1) Check Power of 2
2023-01-31
Lintcode: Topological Sorting
2023-01-31
LintCode_114 不同的路径,115 不同的路径 II
2023-01-31
linux - sftp常用命令介绍
2023-01-31
linux -- ubuntu修改IP地址、网关、dns
2023-01-31
Linux ---> 简单socket
2023-01-31
Linux -chattr -隐藏权限(附加权限)
2023-01-31
Linux /dev/sda3 100%解决
2023-01-31
Linux /dev目录设备文件
2023-01-31
linux 2.6 驱动笔记(一)
2023-01-31
Linux 27岁了!这 27 件相关的有趣事实你可能不知道
2023-01-31
Linux 6 常用工具设置方法
2023-01-31