手写vue

<!DOCTYPE html>
<html>
  <title>test12</title>
  <body id="app">
    <input type="text" v-model="tempAge"/>
    <span v-bind="tempAge" v-click="handleClick"></span>
    <input type="text" v-model="age"/>
    <span v-bind="age"></span>
  </body>
   
<script>
  function Vue (options) {
     this._init(options);
  }
  
  Vue.prototype._init = function (options) {
    this.$data = options.data;
    this.$methods = options.data.methods;
    this.$el = document.querySelector(options.el);
    this.$methods = options.methods;
    this.$key = '';
    
    this._binding = {};
    this._observer(this.$data);
    this._complie(this.$el);
    // this._test(this.$data);
  }
  
  // 观测数据
  Vue.prototype._observer = function (obj) {
    let _this = this;
    for (key in obj) {
      let value;
      if (obj.hasOwnProperty(key)) {
        this._binding[key] = {                                                                                                                                                          
          _directives: []
        };
        value = obj[key];
        if (typeof value === 'object') {
          this._observer(value);
        }
        Object.defineProperty(this.$data, key, {
          enumerable: true,
          configurable: true,
          get: function () {
            console.log(`获取${value}`, key);
            return value;
          },
          set: function (newVal) {
            console.log('key:', key, _this.$key);
            if (value !== newVal) {
              value = newVal;
              _this._binding[_this.$key]._directives.forEach(function (item, index) {
                item.update();
              })
            }
          }
        })
      }
    }
  }
  
  // 为DOM节点添加指令事件
  Vue.prototype._complie = function (root) {
    var _this = this;
    var nodes = root.children;
    for (var i = 0; i < nodes.length; i++) {
      var node = nodes[i];
      if (node.children.length) {
        this._complie(node);
      }
      if (node.hasAttribute('v-click')) {
        node.onclick = (function () {
          var attrVal = nodes[i].getAttribute('v-click');
          return _this.$methods[attrVal].bind(_this.$data);
        })();
      }
      if (node.hasAttribute('v-model') && (node.tagName == 'INPUT' || node.tagName == 'TEXTAREA')) {
        node.addEventListener('input', (function(key) {
          var attrVal = node.getAttribute('v-model');
          _this._binding[attrVal]._directives.push(new Watcher(
            'input',
            node,
            _this,
            attrVal,
            'value'
          ))
          return function() {
            _this.$key = attrVal
            _this.$data[attrVal] =  nodes[key].value;
          }
        })(i));
      } 
      if (node.hasAttribute('v-bind')) {
        var attrVal = node.getAttribute('v-bind');
        _this._binding[attrVal]._directives.push(new Watcher(
          'text',
          node,
          _this,
          attrVal,
          'innerHTML'
        ))
      }
    }
  }
  
   function Watcher(name, el, vm, exp, attr) {
    this.name = name;//指令名称,例如文本节点,该值设为"text"
    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];
  }
  
  Vue.prototype._test = function($data) {
     var a = $data.tempAge;
     $data.tempAge = 32;
  }
  
  window.onload = function () {
    var app = new Vue({
      el: '#app',
      data: {
         tempAge: 88,
         age: 66
      },
      methods: {
        handleClick() {
          console.log(123)
        }
      }
    })
  }

</script>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值