当前位置:首页 > VUE

js 实现vue模板

2026-02-09 13:16:51VUE

使用原生 JavaScript 实现 Vue 模板功能

通过原生 JavaScript 可以实现类似 Vue 的模板功能,包括数据绑定、指令处理和模板渲染。以下是具体实现方法:

数据绑定实现

创建一个简单的数据绑定系统,使用 Object.definePropertyProxy 实现数据响应式:

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

  observe(data) {
    if (!data || typeof data !== 'object') return
    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()
          }
        }
      })
      this.observe(value)
    })
  }
}

class Dep {
  constructor() {
    this.subs = []
  }
  addSub(sub) {
    this.subs.push(sub)
  }
  notify() {
    this.subs.forEach(sub => sub.update())
  }
}

模板编译

实现模板编译功能,处理插值表达式和指令:

class Vue {
  // ...其他代码

  compile() {
    const el = document.querySelector(this.$options.el)
    this.compileNode(el)
  }

  compileNode(node) {
    if (node.nodeType === 1) {
      this.compileElement(node)
    } else if (node.nodeType === 3) {
      this.compileText(node)
    }

    if (node.childNodes && node.childNodes.length) {
      Array.from(node.childNodes).forEach(childNode => {
        this.compileNode(childNode)
      })
    }
  }

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

  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, exp, value => {
            node.value = value
          })
        }
      }
    })
  }
}

观察者模式实现

创建 Watcher 类实现观察者模式:

class Watcher {
  constructor(vm, key, cb) {
    this.vm = vm
    this.key = key
    this.cb = cb
    Dep.target = this
    this.value = vm.$data[key]
    Dep.target = null
  }

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

使用方法示例

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

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

功能扩展

  1. 计算属性:可以通过添加 computed 选项并创建对应的 getter 函数实现
  2. 方法处理:在 Vue 实例上添加 methods 选项,并在模板中绑定事件
  3. 生命周期钩子:在特定阶段触发回调函数
  4. 组件系统:通过创建子类或工厂函数实现组件功能

这种实现方式展示了 Vue 核心功能的基本原理,实际 Vue 源码更加复杂和完善,包含虚拟 DOM、diff 算法等高级特性。

js 实现vue模板

标签: 模板js
分享给朋友:

相关文章

js实现轮播

js实现轮播

实现轮播图的基本思路 轮播图的核心逻辑是通过定时切换展示的图片或内容,通常结合左右滑动按钮和指示器(小圆点)增强交互性。以下是基于原生JavaScript的实现方法。 HTML结构 构建轮播图的HT…

js实现图片上传

js实现图片上传

图片上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现图片上传功能。HTML部分需要创建一个文件选择输入框和一个用于…

js实现全屏

js实现全屏

实现全屏的基本方法 使用JavaScript实现全屏功能主要依赖Element.requestFullscreen()方法。现代浏览器均支持此API,但不同浏览器可能需要添加前缀。 // 触发全屏…

js实现列表

js实现列表

使用 JavaScript 实现列表 JavaScript 提供了多种方式来实现列表功能,包括数组操作、DOM 元素动态生成等。以下是几种常见的实现方法: 使用数组存储列表数据 数组是 JavaS…

js实现预览

js实现预览

文件上传预览实现 使用JavaScript实现文件上传预览功能,可以通过FileReader对象读取文件内容并显示预览。以下代码支持图片、文本和PDF文件的预览: // HTML部分需要包含…

js jquery

js jquery

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互。它兼容多种浏览器,提供易于使用的 API,使开发者能够…