当前位置:首页 > JavaScript

js 实现vue

2026-04-06 19:45:19JavaScript

实现 Vue 的基本功能

使用 JavaScript 实现一个简化版的 Vue,需要核心功能包括数据响应式、模板编译和虚拟 DOM 更新。以下是一个基础实现框架:

class Vue {
  constructor(options) {
    this.$options = options;
    this.$data = options.data();
    this.$el = document.querySelector(options.el);

    this.observe(this.$data);
    this.compile(this.$el);
  }

  observe(data) {
    Object.keys(data).forEach(key => {
      let value = data[key];
      const dep = new Dep();

      Object.defineProperty(data, key, {
        get() {
          Dep.target && dep.addSub(Dep.target);
          return value;
        },
        set(newVal) {
          if (newVal !== value) {
            value = newVal;
            dep.notify();
          }
        }
      });
    });
  }

  compile(node) {
    Array.from(node.childNodes).forEach(child => {
      if (child.nodeType === 3) {
        this.compileText(child);
      } else if (child.nodeType === 1) {
        this.compileElement(child);
        if (child.childNodes.length) {
          this.compile(child);
        }
      }
    });
  }

  compileText(node) {
    const reg = /\{\{(.*?)\}\}/g;
    const text = node.textContent;
    if (reg.test(text)) {
      const key = RegExp.$1.trim();
      node.textContent = this.$data[key];
      new Watcher(this.$data, key, (val) => {
        node.textContent = val;
      });
    }
  }

  compileElement(node) {
    Array.from(node.attributes).forEach(attr => {
      if (attr.name.startsWith('v-')) {
        const dir = attr.name.substring(2);
        const exp = attr.value;
        if (dir === 'model') {
          node.value = this.$data[exp];
          node.addEventListener('input', (e) => {
            this.$data[exp] = e.target.value;
          });
          new Watcher(this.$data, exp, (val) => {
            node.value = val;
          });
        }
      }
    });
  }
}

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

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

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

class Watcher {
  constructor(data, key, cb) {
    Dep.target = this;
    this.cb = cb;
    this.key = key;
    this.data = data;
    this.value = data[key]; // 触发getter
    Dep.target = null;
  }

  update() {
    this.value = this.data[this.key];
    this.cb(this.value);
  }
}

核心实现原理

数据响应式通过 Object.defineProperty 实现,为每个数据属性创建依赖收集器(Dep)。当数据被访问时收集依赖(Watcher),数据变化时通知所有依赖更新。

模板编译通过遍历 DOM 树实现,解析双花括号文本节点和 v-model 指令节点,为每个动态绑定创建对应的 Watcher 实例。

js 实现vue

使用示例

<div id="app">
  <input v-model="message">
  <p>{{ message }}</p>
</div>

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

功能扩展方向

  1. 实现计算属性(computed)
  2. 添加方法(methods)支持
  3. 实现生命周期钩子
  4. 支持组件系统
  5. 实现虚拟 DOM 和 diff 算法

这个实现展示了 Vue 最核心的响应式原理和模板编译机制,实际 Vue 源码更加复杂和完善,包含更多优化和功能特性。

标签: jsvue
分享给朋友:

相关文章

vue实现看板

vue实现看板

Vue 实现看板功能 使用 Vue 实现看板功能可以通过组件化开发、状态管理和拖拽库结合完成。以下是一个完整的实现方案: 基础项目结构 src/ ├── components/ │ ├── Ka…

vue 实现拖拉

vue 实现拖拉

Vue 实现拖拽功能的方法 使用 HTML5 原生拖放 API HTML5 提供了原生的拖放 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 dr…

vue实现hovuer

vue实现hovuer

Vue 实现 Hover 效果 在 Vue 中实现 hover 效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 直接控制 通过 :hover 伪类实现,无需额外的 JavaScrip…

vue实现爬虫

vue实现爬虫

Vue 实现爬虫的基本思路 Vue.js 本身是一个前端框架,主要用于构建用户界面。要实现爬虫功能,通常需要结合后端技术或浏览器自动化工具。以下是几种常见的方法: 方法一:Vue + Node.js…

proxy实现vue

proxy实现vue

使用 Proxy 实现 Vue 响应式系统 Vue 3 的响应式系统基于 JavaScript 的 Proxy API,通过代理对象实现对属性的拦截和依赖追踪。以下是实现的核心逻辑: 创建响…

vue实现keepalive

vue实现keepalive

Vue 中实现 keep-alive 在 Vue 中,keep-alive 是一个内置组件,用于缓存动态组件或组件的状态,避免重复渲染和销毁。以下是实现 keep-alive 的几种方法: 基本用法…