亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

動手擼起來,ES6實現簡單的vue

//模板

<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="style.css">
  <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="script.js"></script>
</head>

<body>
  <div id="test">
    <ol>
      <li>v-model:
        <input v-model="msg" placeholder="please...">
      </li>
      <li>v-text:<mark v-text="msg"></mark</li>
      <li >v-show :<mark v-show="isShow">hide or show</mark></li>
      <li>v-on:click:
        <button v-on:click="changeShow"  v-text="btn"></button>
      </li>
    </ol>
  </div>

  <script>
    var vm = new Vue('#test', {
      msg: '',
      isShow: true,
      btn: 'toggle',
      changeShow() {
        vm.$data.isShow = !vm.$data.isShow;
      }
    });
  </script>
</body>

</html>

//逻辑

//declaring directives
var Directives = {
    text(el, value) {
        el.textContent = value || ''
      },
      show(el, value) {
        el.style.display = value ? '' : 'none'
      },
      on: {
        update(el, handler, event, directive) {
          if (!directive.handlers) {
            directive.handlers = {}
          }
          var handlers = directive.handlers
          if (handlers[event]) {
            el.removeEventListener(event, handlers[event])
          }
          if (handler) {
            handler = handler.bind(el);
            el.addEventListener(event, handler);
            handlers[event] = handler;
          }
        }
      },
      model: {
        bind(el, key, directive, seed) {
            el.addEventListener('keyup', () => seed.$data[key] = el.value);
          },
          update(el, value) {
            el.value = value;
          }
      }
  }
  //difine utils
var Utils = {
  cloneAttributes(attributes) {
      return Array.from(attributes).map(attribute => {
        return {
          name: attribute.name,
          value: attribute.value
        }
      })
    },
    parseDirective(attr) {
      if (!attr.name.includes(prefix)) return;
      var noprefix = attr.name.slice(prefix.length + 1),
        argIndex = noprefix.indexOf(':'),
        dirname = argIndex === -1 ? noprefix : noprefix.slice(0, argIndex),
        def = Directives[dirname],
        arg = argIndex === -1 ? null : noprefix.slice(argIndex + 1);
      var exp = attr.value,
        key = exp.trim();
      return def ? {
        attr: attr,
        key: key,
        definition: def,
        argument: arg,
        update: typeof def === 'function' ? def : def.update,
        bind: typeof def === 'function' ? null : def.bind ? def.bind : null
      } : null;
    }
};
//prefix
var prefix = 'v',
  selector = Object.keys(Directives).map(d => `[${prefix}-$p8pr12h]`).join();
//constructor
function Vue(el, opts = {}) {
  var root = self.$el = document.querySelector(el),
    els = root.querySelectorAll(selector),
    _bindings = {};
  this.$opts = opts;
  this.$data = {}; //export api
  this.processNode(els, _bindings);
  this.initData(_bindings);
}
//handle nodes
Vue.prototype.processNode = function(els, _bindings) {
    els.forEach(el => {
      Utils.cloneAttributes(el.attributes).forEach(attr => {
        var directive = Utils.parseDirective(attr);
        directive && this.bindDirective(el, _bindings, directive)
      });
    });
  }
  //attribute remove and directive binding
Vue.prototype.bindDirective = function(el, _bindings, directive) {
    var self = this;
    el.removeAttribute(directive.attr.name);
    var key = directive.key,
      binding = _bindings[key];
    if (!binding) {
      _bindings[key] = binding = {
        value: undefined,
        directives: []
      }
    }
    directive.el = el;
    binding.directives.push(directive);

    if (directive.bind) {
      directive.bind(el, key, directive, self);
    }
    if (!self.$data.hasOwnProperty(key)) {
      self.bind(key, binding);
    }
  }
  //set and get
Vue.prototype.bind = function(key, binding) {
  Object.defineProperty(this.$data, key, {
    set: value => {
      binding.value = value;
      binding.directives.forEach(directive => {
        directive.update(
          directive.el,
          value,
          directive.argument,
          directive,
          this
        )
      })
    },
    get: () => binding.value,
  })
};
//initial
Vue.prototype.initData = function(_bindings) {
  for (let variable in _bindings) {
    this.$data[variable] = this.$opts[variable];
  }
};
點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
Web前端工程師
手記
粉絲
7245
獲贊與收藏
3475

關注作者,訂閱最新文章

閱讀免費教程

  • 推薦
  • 1
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消