当前位置:首页 > VUE

自己实现vue双括号

2026-01-23 13:31:27VUE

实现 Vue 双括号插值的基本原理

Vue 的双括号插值({{ }})是数据绑定的核心功能之一,其本质是将模板中的表达式解析并与数据对象动态关联。以下是手动实现类似功能的步骤:

数据劫持与响应式

通过 Object.definePropertyProxy 实现数据劫持,监听数据变化:

function observe(data) {
  if (!data || typeof data !== 'object') return;
  Object.keys(data).forEach(key => {
    let value = data[key];
    const dep = new Dep(); // 依赖收集器
    observe(value); // 递归劫持嵌套对象
    Object.defineProperty(data, key, {
      get() {
        Dep.target && dep.addSub(Dep.target); // 收集依赖
        return value;
      },
      set(newVal) {
        if (newVal !== value) {
          value = newVal;
          observe(newVal); // 新值也可能是对象
          dep.notify(); // 触发更新
        }
      }
    });
  });
}

依赖收集与发布订阅

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

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]; // 触发 getter
    Dep.target = null;
  }
  update() {
    const newValue = this.vm[this.key];
    if (newValue !== this.oldValue) {
      this.cb(newValue);
      this.oldValue = newValue;
    }
  }
}

模板解析与编译

通过正则匹配 {{ }} 并替换为数据:

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

  const regex = /\{\{(.*?)\}\}/g;
  function replaceText(node) {
    if (node.nodeType === 3) { // 文本节点
      const text = node.textContent;
      if (regex.test(text)) {
        const key = RegExp.$1.trim();
        new Watcher(vm, key, val => {
          node.textContent = text.replace(regex, val);
        });
        node.textContent = text.replace(regex, vm[key]);
      }
    }
    if (node.childNodes) {
      node.childNodes.forEach(replaceText);
    }
  }
  replaceText(fragment);
  el.appendChild(fragment);
}

初始化实例

整合以上逻辑到主类中:

class MiniVue {
  constructor(options) {
    this.$data = options.data();
    observe(this.$data);
    this.proxyData();
    compile(options.el, this);
  }
  proxyData() {
    Object.keys(this.$data).forEach(key => {
      Object.defineProperty(this, key, {
        get() {
          return this.$data[key];
        },
        set(val) {
          this.$data[key] = val;
        }
      });
    });
  }
}

使用示例

<div id="app">{{ message }}</div>
<script>
  new MiniVue({
    el: document.getElementById('app'),
    data: () => ({ message: 'Hello World' })
  });
</script>

关键点说明

  • 数据劫持:通过 Object.defineProperty 拦截属性的读写操作。
  • 依赖收集:在 getter 中收集依赖(Watcher),在 setter 中触发更新。
  • 模板编译:递归遍历 DOM 节点,替换 {{ }} 为实际数据并建立绑定。
  • 性能优化:实际 Vue 还包含虚拟 DOM、异步更新队列等机制,此处仅展示核心原理。

自己实现vue双括号

标签: 括号vue
分享给朋友:

相关文章

vue登录业务的实现

vue登录业务的实现

实现登录业务的基本流程 在Vue中实现登录业务通常涉及前端界面设计、表单验证、API请求交互、状态管理以及路由控制等环节。以下是核心实现步骤: 登录表单设计与验证 创建包含用户名和密码输入框的登录…

vue状态管理怎么实现

vue状态管理怎么实现

Vue 状态管理实现方法 使用 Vuex(官方推荐) Vuex 是 Vue 的官方状态管理库,适合中大型应用。 安装 Vuex: npm install vuex --save 创建 Store…

vue实现3d宇宙

vue实现3d宇宙

Vue实现3D宇宙效果 使用Three.js库 Three.js是一个强大的JavaScript 3D库,可以轻松集成到Vue项目中。安装Three.js依赖: npm install three…

vue聊天实现

vue聊天实现

Vue 聊天应用实现步骤 创建 Vue 项目 使用 Vue CLI 或 Vite 初始化项目,安装必要依赖(如 Vue 3、Element UI 或 Vuetify 用于 UI 组件)。 npm…

vue  select实现

vue select实现

Vue Select 实现方法 在 Vue 中实现下拉选择功能可以使用原生 <select> 标签或第三方库如 vue-select。以下是两种方法的详细说明: 原生 HTML Sele…

vue实现sso

vue实现sso

Vue 实现 SSO(单点登录)方案 单点登录(SSO)允许用户通过一次登录访问多个相关系统。以下是基于 Vue 的实现方案: 使用 OAuth2/OpenID Connect 协议 OAuth2…