当前位置:首页 > VUE

简易实现vue

2026-02-09 10:19:34VUE

简易 Vue 实现核心思路

创建一个极简的 Vue 实现需要理解其核心机制:数据响应式、模板编译和虚拟 DOM。以下是一个简化版实现框架,仅包含核心功能。

数据响应式系统

通过 Object.defineProperty 实现数据劫持,这是 Vue 2.x 的响应式原理基础:

class Observer {
  constructor(data) {
    this.walk(data)
  }

  walk(data) {
    Object.keys(data).forEach(key => {
      this.defineReactive(data, key, data[key])
    })
  }

  defineReactive(obj, key, val) {
    const dep = new Dep()
    Object.defineProperty(obj, key, {
      get() {
        Dep.target && dep.addSub(Dep.target)
        return val
      },
      set(newVal) {
        if (newVal === val) return
        val = newVal
        dep.notify()
      }
    })
  }
}

依赖收集系统

实现简单的发布-订阅模式来管理依赖:

class Dep {
  constructor() {
    this.subs = []
  }

  addSub(sub) {
    this.subs.push(sub)
  }

  notify() {
    this.subs.forEach(sub => sub.update())
  }
}

Dep.target = null

编译模板

实现最基础的模板编译功能,将 {{}} 语法转换为数据绑定:

function compile(el, vm) {
  vm.$el = document.querySelector(el)
  const fragment = document.createDocumentFragment()
  let child
  while (child = vm.$el.firstChild) {
    fragment.appendChild(child)
  }

  function replace(frag) {
    Array.from(frag.childNodes).forEach(node => {
      const txt = node.textContent
      const reg = /\{\{(.*?)\}\}/g

      if (node.nodeType === 3 && reg.test(txt)) {
        const arr = RegExp.$1.split('.')
        let val = vm
        arr.forEach(key => {
          val = val[key]
        })
        node.textContent = txt.replace(reg, val).trim()

        new Watcher(vm, RegExp.$1, newVal => {
          node.textContent = txt.replace(reg, newVal).trim()
        })
      }

      if (node.childNodes && node.childNodes.length) {
        replace(node)
      }
    })
  }

  replace(fragment)
  vm.$el.appendChild(fragment)
}

观察者实现

创建 Watcher 类来连接响应式数据和视图更新:

class Watcher {
  constructor(vm, exp, cb) {
    this.vm = vm
    this.exp = exp
    this.cb = cb
    Dep.target = this
    let val = vm
    exp.split('.').forEach(key => {
      val = val[key]
    })
    Dep.target = null
  }

  update() {
    let val = this.vm
    this.exp.split('.').forEach(key => {
      val = val[key]
    })
    this.cb(val)
  }
}

主类实现

组合以上功能创建 MiniVue 类:

class MiniVue {
  constructor(options) {
    this.$options = options
    this._data = options.data
    new Observer(this._data)
    Object.keys(this._data).forEach(key => {
      this._proxy(key)
    })
    compile(options.el, this)
  }

  _proxy(key) {
    Object.defineProperty(this, key, {
      configurable: true,
      enumerable: true,
      get: () => this._data[key],
      set: val => {
        this._data[key] = val
      }
    })
  }
}

使用示例

这样实现的简易 Vue 可以这样使用:

简易实现vue

<div id="app">
  {{ message }}
</div>

<script>
new MiniVue({
  el: '#app',
  data: {
    message: 'Hello Mini Vue!'
  }
})
</script>

这个实现包含了 Vue 最核心的响应式系统、模板编译和依赖收集机制。实际 Vue 源码还包含虚拟 DOM diff、组件系统等更多复杂功能,但这个简化版已经展示了核心原理。

标签: 简易vue
分享给朋友:

相关文章

vue实现模块

vue实现模块

Vue 实现模块化的方法 Vue 支持多种模块化开发方式,可以根据项目需求选择适合的方案。 使用单文件组件(SFC) 单文件组件是 Vue 最常用的模块化方式,将模板、脚本和样式封装在一个 .vue…

vue轮播实现

vue轮播实现

Vue 轮播实现方法 使用第三方库(推荐) 安装 swiper 或 vue-awesome-swiper 库,快速实现功能丰富的轮播效果。 以 vue-awesome-swiper 为例: npm…

vue实现pc

vue实现pc

Vue 实现 PC 端应用开发 Vue.js 是一个流行的前端框架,适用于构建 PC 端 Web 应用。以下是关键步骤和最佳实践: 项目初始化 使用 Vue CLI 或 Vite 创建项目:…

vue实现画廊

vue实现画廊

Vue实现画廊的基本方法 使用Vue.js实现画廊功能可以通过多种方式完成,以下是一种常见的实现方案: 安装必要依赖 npm install vue-gallery --save 引入组件并注册…

vue实现队列

vue实现队列

Vue 实现队列功能 在 Vue 中实现队列功能可以通过多种方式完成,以下是几种常见的方法: 使用数组模拟队列 队列遵循先进先出(FIFO)原则,可以用数组的 push 和 shift 方法模拟入队…

vue 实现音乐

vue 实现音乐

Vue 实现音乐播放功能 在 Vue 中实现音乐播放功能,可以通过 HTML5 的 <audio> 元素或第三方库如 howler.js 来实现。以下是两种方法的详细步骤: 使用 HTM…