自定义实现vue
自定义实现 Vue 的核心原理
实现一个简化版的 Vue 需要理解其核心机制:数据响应式、模板编译、虚拟 DOM 和依赖收集。以下是关键实现步骤:
数据响应式系统
通过 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
}
}
模板编译
实现简单的模板编译器,将模板转换为渲染函数:
class Compiler {
constructor(el, vm) {
this.$vm = vm
this.$el = document.querySelector(el)
this.compile(this.$el)
}
compile(node) {
const childNodes = node.childNodes
Array.from(childNodes).forEach(child => {
if (this.isElement(child)) {
this.compileElement(child)
} else if (this.isInterpolation(child)) {
this.compileText(child)
}
if (child.childNodes && child.childNodes.length > 0) {
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) {
const attrs = node.attributes
Array.from(attrs).forEach(attr => {
if (this.isDirective(attr.name)) {
const dir = attr.name.substring(2)
this[dir] && this[dir](node, attr.value)
}
})
}
isDirective(attr) {
return attr.startsWith('v-')
}
text(node, exp) {
this.update(node, exp, 'text')
}
html(node, exp) {
this.update(node, exp, 'html')
}
update(node, exp, dir) {
const updater = this[`${dir}Updater`]
updater && updater(node, this.$vm[exp])
new Watcher(this.$vm, exp, value => {
updater && updater(node, value)
})
}
textUpdater(node, value) {
node.textContent = value
}
htmlUpdater(node, value) {
node.innerHTML = value
}
}
整合实现
创建 Vue 类整合以上功能:
class Vue {
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>
<button v-text="btnText"></button>
</div>
<script>
const app = new Vue({
el: '#app',
data: {
message: 'Hello, Vue!',
btnText: 'Click me'
}
})
</script>
实现注意事项
- 现代 Vue 3 使用 Proxy 替代
Object.defineProperty实现响应式 - 真实实现需要考虑更多边界条件和性能优化
- 虚拟 DOM diff 算法是性能关键
- 组件系统需要额外实现插槽、生命周期等机制
这个简化实现展示了 Vue 的核心原理,真实框架需要处理更多复杂场景和优化。







