美文网首页
实现简单Vue双向绑定

实现简单Vue双向绑定

作者: 周霖_ | 来源:发表于2018-04-19 16:50 被阅读0次

主要思路通过改造data数据的set方法,当数据发生变时(v-mode,v-click)通知watcher去执行update

  • js代码
    function zlVue(options){
            this.init(options)
        }
        //初始化构造函数
        zlVue.prototype.init = function(options){
            this.$el = document.querySelector(options.el);
            this.$data = options.data;
            this.$methods = options.methods;
            this._bind = {} 
            this._initDate(this.$data);
            this._initEl(this.$el)
        }
        //重写data的set和get函数
        zlVue.prototype._initDate = function(obj){
            for(key in obj){
                this._bind[key] = {
                    _funArr:[]
                }
                var value = obj[key]
                if(typeof value ==='object'){
                this._initDate(value)
                }
            }
             var _this = this
             //set方法中,只要值发生改变就执行update()去更新视图
            Object.defineProperty(this.$data,key,{
                enumerable:true,
                configurable:true,
                get: function(){
                    return value
                },
                set: function(newValue){
                    if(newValue !== value){
                        value = newValue;
                        _this._bind[key]._funArr.forEach(function(v){
                            v.update()
                        })
                    }
                }
            })
        }
        //对页面进行初始化
        zlVue.prototype._initEl = function(el){
            var _this = this
            var nodes = el.children;
            for(var i = 0;i < nodes.length; i++){
                var node = nodes[i]
                if(node.length){
                    this._initEl(node);
                }
                //对绑定的data,进行状态
                if(node.hasAttribute('v-click')){
                    node.onclick = (function(){
                    var funName = node.getAttribute('v-click');
                        return _this.$methods[funName].bind(_this.$data)
                    })();
                }
                if(node.hasAttribute('v-bind')){
                    var dataName = node.getAttribute('v-bind');
                    _this._bind[dataName]._funArr.push(new Watcher(node,_this,dataName,'innerHTML'))
                }
                if(node.hasAttribute('v-model') && (node.tagName === 'INPUT' || nodoe.tagName === 'TEXTAREA')){
                    node.addEventListener('input',(function(key) {
                        var dataName = node.getAttribute('v-model');
                        _this._bind[dataName]._funArr.push(new Watcher(
                            node,
                            _this,
                            dataName,
                            'value'
                            ))

                        return  function() {
                            _this.$data[dataName] = nodes[key].value;
                        }
                    })(i))
                }
            }
        }
        //数据变化是实现对DOM元素的更新:
        function Watcher(el,vm,exp,attr){
            this.el =el,
            this.vm = vm,
            this.exp = exp,
            this.attr = attr,
            this.update()
        }
        Watcher.prototype.update = function(){
            this.el[this.attr] = this.vm.$data[this.exp];
        }
  • 实例化
    window.onload = function(){
            var app = new zlVue({
                el:'#app',
                data: {
                    number: 0
                },
                methods: {
                    inerement: function(){
                        this.number ++;
                    },
                }
            })
        }
  • html代码
<div id="app">
    <form>
      <input type="text"  v-model="number">
      <button type="button" v-click="inerement">add</button>
    </form>
    <h3 v-bind="number"></h3>
    </div>
  • 实际效果图


    image.png
    image.png

相关文章

  • 深入Vue响应式原理

    1.Vue的双向数据绑定 参考 vue的双向绑定原理及实现Vue双向绑定的实现原理Object.definepro...

  • Vue实现数据双向绑定的原理

    Vue实现数据双向绑定的原理:Object.defineProperty() vue实现数据双向绑定主要是:采用数...

  • vue 双向数据绑定

    Vue实现数据双向绑定的原理:Object.defineProperty()vue实现数据双向绑定主要是:采用数据...

  • js实现双向数据绑定

    js双向绑定几种方法的介绍 使用Object.defineProperty实现简单的js双向绑定剖析Vue原理&实...

  • Vue框架基础

    原生js与Vue框架的区别 用原生实现双向数据绑定 用Vue实现双向数据绑定 Vue是一个javaScript框架...

  • Vue2.0原理与MVVM的实现

    剖析Vue原理&实现双向绑定MVVM vue源码 双向绑定 -- MVVM 目前几种主流的MVC框架都实现了单向数...

  • 【转】JavaScript的观察者模式(Vue双向绑定原理)

    关于Vue实现数据双向绑定的原理,请点击:Vue实现数据双向绑定的原理原文链接:JavaScript设计模式之观察...

  • 前端理论面试--VUE

    vue双向绑定的原理(详细链接) VUE实现双向数据绑定的原理就是利用了 Object.definePropert...

  • Vue双向数据绑定原理

    剖析Vue实现原理 - 如何实现双向绑定mvvm 本文能帮你做什么?1、了解vue的双向数据绑定原理以及核心代码模...

  • 关于双向绑定的问题

    剖析Vue实现原理 - 如何实现双向绑定mvvm 本文能帮你做什么?1、了解vue的双向数据绑定原理以及核心代码模...

网友评论

      本文标题:实现简单Vue双向绑定

      本文链接:https://www.haomeiwen.com/subject/awgwkftx.html