用js实现vue
用 JavaScript 实现 Vue 核心功能
Vue.js 是一个渐进式框架,其核心功能包括响应式数据绑定、虚拟 DOM 和组件系统。以下是一个简化版的实现,展示如何用原生 JavaScript 模拟 Vue 的核心机制。
响应式数据系统
通过 Object.defineProperty 或 Proxy 实现数据劫持,监听数据变化并触发更新:

class Observer {
constructor(data) {
this.observe(data);
}
observe(data) {
if (!data || typeof data !== 'object') return;
Object.keys(data).forEach(key => {
this.defineReactive(data, key, data[key]);
});
}
defineReactive(obj, key, value) {
const dep = new Dep();
this.observe(value); // 递归监听嵌套对象
Object.defineProperty(obj, key, {
get() {
Dep.target && dep.addSub(Dep.target);
return value;
},
set(newVal) {
if (newVal === value) return;
value = newVal;
dep.notify(); // 触发更新
}
});
}
}
class Dep {
constructor() {
this.subs = [];
}
addSub(sub) {
this.subs.push(sub);
}
notify() {
this.subs.forEach(sub => sub.update());
}
}
编译模板与指令解析
模拟 Vue 的模板编译过程,解析 v-model 等指令:

class Compiler {
constructor(el, vm) {
this.$vm = vm;
this.$el = document.querySelector(el);
this.compile(this.$el);
}
compile(node) {
Array.from(node.childNodes).forEach(child => {
if (this.isElement(child)) {
this.compileElement(child);
} else if (this.isInterpolation(child)) {
this.compileText(child);
}
if (child.childNodes.length) {
this.compile(child);
}
});
}
isElement(node) {
return node.nodeType === 1;
}
isInterpolation(node) {
return node.nodeType === 3 && /\{\{(.*)\}\}/.test(node.textContent);
}
compileText(node) {
const exp = RegExp.$1.trim();
this.update(node, exp, 'text');
}
compileElement(node) {
Array.from(node.attributes).forEach(attr => {
if (attr.name.startsWith('v-')) {
const dir = attr.name.substring(2);
this[dir] && this[dir](node, attr.value);
}
});
}
text(node, exp) {
this.update(node, exp, 'text');
}
model(node, exp) {
this.update(node, exp, 'model');
node.addEventListener('input', e => {
this.$vm[exp] = e.target.value;
});
}
update(node, exp, dir) {
const fn = this[`${dir}Updater`];
fn && fn(node, this.$vm[exp]);
new Watcher(this.$vm, exp, val => {
fn && fn(node, val);
});
}
textUpdater(node, value) {
node.textContent = value;
}
modelUpdater(node, value) {
node.value = value;
}
}
实现 Watcher
连接响应式数据和视图更新的桥梁:
class Watcher {
constructor(vm, key, cb) {
this.$vm = vm;
this.key = key;
this.cb = cb;
Dep.target = this;
this.$vm[this.key]; // 触发getter,收集依赖
Dep.target = null;
}
update() {
this.cb.call(this.$vm, this.$vm[this.key]);
}
}
整合为 MiniVue
将上述模块组合成一个简易 Vue 类:
class MiniVue {
constructor(options) {
this.$options = options;
this.$data = options.data();
new Observer(this.$data);
this.proxyData(this.$data);
new Compiler(options.el, this);
}
proxyData(data) {
Object.keys(data).forEach(key => {
Object.defineProperty(this, key, {
get() {
return data[key];
},
set(newVal) {
data[key] = newVal;
}
});
});
}
}
使用示例
<div id="app">
<p>{{ message }}</p>
<input v-model="message">
</div>
<script>
const app = new MiniVue({
el: '#app',
data() {
return {
message: 'Hello MiniVue!'
};
}
});
</script>
关键点说明
- 响应式系统:通过数据劫持监听属性变化,使用发布-订阅模式通知依赖更新。
- 模板编译:解析 DOM 节点,识别指令和插值表达式,绑定更新函数。
- 虚拟 DOM 简化:示例中直接操作真实 DOM,实际 Vue 使用虚拟 DOM 进行高效 diff。
- 组件系统:完整实现需支持组件树、生命周期等,此处仅展示核心数据绑定。
以上代码展示了 Vue 的核心原理,实际 Vue.js 的实现更为复杂,包含优化策略、更多指令支持和完整的生命周期管理。






