自己实现vue
实现简易版 Vue 的核心思路
Vue 的核心是通过数据劫持和发布-订阅模式实现数据驱动视图。以下是实现一个简易版 Vue 的关键步骤:
数据劫持(响应式系统)
使用 Object.defineProperty 或 Proxy 拦截数据的读取和修改操作。当数据变化时,通知依赖的视图更新。
class Observer {
constructor(data) {
this.walk(data);
}
walk(data) {
if (!data || typeof data !== 'object') return;
Object.keys(data).forEach(key => {
this.defineReactive(data, key, data[key]);
});
}
defineReactive(obj, key, val) {
const dep = new Dep();
this.walk(val);
Object.defineProperty(obj, key, {
enumerable: true,
configurable: true,
get() {
Dep.target && dep.addSub(Dep.target);
return val;
},
set(newVal) {
if (newVal === val) return;
val = newVal;
dep.notify();
}
});
}
}
依赖收集(发布-订阅模式)
通过 Dep 和 Watcher 实现依赖收集和通知更新。
class Dep {
constructor() {
this.subs = [];
}
addSub(sub) {
this.subs.push(sub);
}
notify() {
this.subs.forEach(sub => sub.update());
}
}
class Watcher {
constructor(vm, key, cb) {
this.vm = vm;
this.key = key;
this.cb = cb;
Dep.target = this;
this.oldValue = vm[key];
Dep.target = null;
}
update() {
const newValue = this.vm[this.key];
if (newValue === this.oldValue) return;
this.cb(newValue);
this.oldValue = newValue;
}
}
模板编译
解析模板中的指令(如 v-model)和插值表达式(如 {{}}),并建立数据与视图的绑定关系。
class Compiler {
constructor(el, vm) {
this.el = document.querySelector(el);
this.vm = vm;
this.compile(this.el);
}
compile(node) {
const childNodes = node.childNodes;
[...childNodes].forEach(child => {
if (this.isElementNode(child)) {
this.compileElement(child);
} else if (this.isTextNode(child)) {
this.compileText(child);
}
if (child.childNodes && child.childNodes.length) {
this.compile(child);
}
});
}
isElementNode(node) {
return node.nodeType === 1;
}
isTextNode(node) {
return node.nodeType === 3;
}
compileElement(node) {
const attributes = node.attributes;
[...attributes].forEach(attr => {
const { name, value } = attr;
if (this.isDirective(name)) {
const [, directive] = name.split('-');
const [dirName, eventName] = directive.split(':');
compileUtil[dirName] && compileUtil[dirName](node, value, this.vm, eventName);
}
});
}
compileText(node) {
const content = node.textContent;
if (/\{\{(.+?)\}\}/.test(content)) {
compileUtil['text'](node, content, this.vm);
}
}
isDirective(attrName) {
return attrName.startsWith('v-');
}
}
实现 Vue 类
整合以上功能,创建 Vue 类作为入口。

class Vue {
constructor(options) {
this.$options = options;
this.$data = options.data;
this.observe(this.$data);
this.proxyData(this.$data);
new Compiler(options.el, this);
}
observe(data) {
if (!data || typeof data !== 'object') return;
new Observer(data);
}
proxyData(data) {
Object.keys(data).forEach(key => {
Object.defineProperty(this, key, {
get() {
return data[key];
},
set(newVal) {
data[key] = newVal;
}
});
});
}
}
使用示例
<div id="app">
<input v-model="message" />
<p>{{ message }}</p>
</div>
<script>
const app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!'
}
});
</script>
关键点总结
- 数据劫持:通过
Object.defineProperty或Proxy监听数据变化。 - 依赖收集:在
getter中收集依赖,在setter中触发更新。 - 模板编译:解析模板中的指令和插值表达式,建立数据与视图的绑定。
- Watcher:作为桥梁连接数据劫持和模板编译,触发更新回调。
以上实现了一个简易版的 Vue,包含了核心的响应式系统和模板编译功能。实际 Vue 的实现更为复杂,包括虚拟 DOM、组件系统等更多功能。






