当前位置:首页 > VUE

实现简易vue

2026-02-10 02:05:41VUE

实现简易 Vue 的核心思路

简易 Vue 的实现主要围绕数据响应式、模板编译和依赖收集展开。以下是一个简化版 Vue 的实现框架:

数据响应式系统

通过 Object.definePropertyProxy 实现数据劫持:

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()
      }
    })
  }
}

依赖收集系统

实现简单的发布-订阅模式:

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

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

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

Dep.target = null

编译器实现

简单模板编译,将模板中的插值表达式替换为数据:

class Compiler {
  constructor(el, vm) {
    this.$vm = vm
    this.$el = document.querySelector(el)

    if (this.$el) {
      this.compile(this.$el)
    }
  }

  compile(el) {
    const childNodes = el.childNodes
    Array.from(childNodes).forEach(node => {
      if (this.isElement(node)) {
        this.compileElement(node)
      } else if (this.isInterpolation(node)) {
        this.compileText(node)
      }

      if (node.childNodes && node.childNodes.length > 0) {
        this.compile(node)
      }
    })
  }

  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')
  }

  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
  }
}

观察者实现

连接响应式数据和视图更新的桥梁:

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])
  }
}

主框架整合

将各个模块组合成简易 Vue 类:

实现简易vue

class Vue {
  constructor(options) {
    this.$options = options
    this.$data = options.data

    // 数据响应式
    new Observer(this.$data)

    // 代理data到Vue实例
    Object.keys(this.$data).forEach(key => {
      this._proxy(key)
    })

    // 编译模板
    new Compiler(options.el, this)
  }

  _proxy(key) {
    Object.defineProperty(this, key, {
      get() {
        return this.$data[key]
      },
      set(newVal) {
        this.$data[key] = newVal
      }
    })
  }
}

使用示例

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

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

  setTimeout(() => {
    vm.message = 'Data changed!'
  }, 1000)
</script>

这个简易实现包含了 Vue 最核心的响应式系统、模板编译和依赖收集机制。实际 Vue 的实现要复杂得多,包括虚拟 DOM、组件系统、指令系统等更多功能。

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

相关文章

vue实现granfana

vue实现granfana

Vue 实现类似 Grafana 的仪表盘 要在 Vue 中实现类似 Grafana 的仪表盘功能,需要结合数据可视化库、状态管理和 UI 组件。以下是关键步骤和推荐工具: 数据可视化库 使用 E…

vue实现回复

vue实现回复

Vue 实现回复功能 在 Vue 中实现回复功能通常涉及前端表单交互、数据绑定和后端通信。以下是实现回复功能的详细步骤: 数据绑定与表单设计 使用 Vue 的 v-model 绑定回复框的输入内容,…

vue如何实现登录

vue如何实现登录

实现登录功能的基本步骤 使用Vue实现登录功能通常需要结合后端API、状态管理以及路由控制。以下是常见的实现方式: 创建登录表单组件 在Vue组件中构建包含用户名和密码输入框的表单: <te…

vue实现选择框

vue实现选择框

Vue 实现选择框的方法 Vue 中可以通过多种方式实现选择框(下拉框),包括原生 HTML 的 <select> 元素结合 Vue 的数据绑定,或者使用第三方 UI 库如 Element…

vue实现桌面widget

vue实现桌面widget

Vue 实现桌面 Widget 的方法 使用 Vue 实现桌面 Widget 通常需要结合 Electron 或 NW.js 等桌面应用框架,以及系统级 API 调用。以下是具体实现方法: 使用 E…

vue router 实现onshow

vue router 实现onshow

Vue Router 实现 onShow 功能 Vue Router 本身没有直接提供 onShow 这样的生命周期钩子,但可以通过以下方法模拟类似功能。 使用导航守卫 导航守卫可以在路由切换时触发…