欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

vue中complie數(shù)據(jù)雙向綁定原理

這篇文章主要介紹“vue中complie數(shù)據(jù)雙向綁定原理”,在日常操作中,相信很多人在vue中complie數(shù)據(jù)雙向綁定原理問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”vue中complie數(shù)據(jù)雙向綁定原理”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

創(chuàng)新互聯(lián)公司是一家以網(wǎng)絡(luò)技術(shù)公司,為中小企業(yè)提供網(wǎng)站維護(hù)、成都網(wǎng)站制作、做網(wǎng)站、網(wǎng)站備案、服務(wù)器租用、申請(qǐng)域名、軟件開發(fā)、成都微信小程序等企業(yè)互聯(lián)網(wǎng)相關(guān)業(yè)務(wù),是一家有著豐富的互聯(lián)網(wǎng)運(yùn)營(yíng)推廣經(jīng)驗(yàn)的科技公司,有著多年的網(wǎng)站建站經(jīng)驗(yàn),致力于幫助中小企業(yè)在互聯(lián)網(wǎng)讓打出自已的品牌和口碑,讓企業(yè)在互聯(lián)網(wǎng)上打開一個(gè)面向全國(guó)乃至全球的業(yè)務(wù)窗口:建站歡迎聯(lián)系:18980820575

vue數(shù)據(jù)雙向綁定原理,和簡(jiǎn)單的實(shí)現(xiàn),本文將實(shí)現(xiàn)mvvm的模板指令解析器

vue中complie數(shù)據(jù)雙向綁定原理

1)vue數(shù)據(jù)雙向綁定原理-observer

2)vue數(shù)據(jù)雙向綁定原理-wather

3)vue數(shù)據(jù)雙向綁定原理-解析器 Complie

vue數(shù)據(jù)雙向綁定原理,和簡(jiǎn)單的實(shí)現(xiàn),本文將實(shí)現(xiàn)mvvm的模板指令解析器

上一步實(shí)現(xiàn)了簡(jiǎn)單數(shù)據(jù)綁定,最后實(shí)現(xiàn)解析器,來解析v-model,v-on:click等指令,和{{}}模板數(shù)據(jù)。解析器Compile實(shí)現(xiàn)步驟:

  • 解析模板指令,并替換模板數(shù)據(jù),初始化視圖

  • 將模板指令對(duì)應(yīng)的節(jié)點(diǎn)綁定對(duì)應(yīng)的更新函數(shù),初始化相應(yīng)的訂閱器

為了解析模板,首先需要獲取到dom元素,然后對(duì)含有dom元素上含有指令的節(jié)點(diǎn)進(jìn)行處理,因此這個(gè)環(huán)節(jié)需要對(duì)dom操作比較頻繁,所有可以先建一個(gè)fragment片段,將需要解析的dom節(jié)點(diǎn)存入fragment片段里再進(jìn)行處理:

function node2Fragment(el) {
  var fragment = document.createDocumentFragment(),
    child;
  // 將原生節(jié)點(diǎn)拷貝到fragment
  while ((child = el.firstChild)) {
    fragment.appendChild(child);
  }

  return fragment;
}

接下來渲染'{{}}'模板

//Compile
function Compile(el, vm) {
  this.$vm = vm;
  this.$el = this.isElementNode(el) ? el : document.querySelector(el);
  if (this.$el) {
    this.$fragment = this.node2Fragment(this.$el);
    this.init();
    this.$el.appendChild(this.$fragment);
  }
}

Compile.prototype = {
  init: function () {
    this.compileElement(this.$fragment);
  },

  node2Fragment: function (el) {
    //...
  },

  //編譯模板
  compileElement: function (el) {
    var childNodes = el.childNodes,
      self = this;
    [].slice.call(childNodes).forEach(function (node) {
      var text = node.textContent;
      var reg = /{{(.*)}}/; //表達(dá)式文本
      //按元素節(jié)點(diǎn)方式編譯
      if (self.isElementNode(node)) {
        self.compile(node);
      } else if (self.isTextNode(node) && reg.test(text)) {
        self.compileText(node, RegExp.$1);
      }
      //遍歷編譯子節(jié)點(diǎn)
      if (node.childNodes && node.childNodes.length) {
        self.compileElement(node);
      }
    });
  },

  isElementNode: function (node) {
    return node.nodeType == 1;
  },

  isTextNode: function (node) {
    return node.nodeType == 3;
  },

  compileText: function (node, exp) {
    var self = this;
    var initText = this.$vm[exp];
    this.updateText(node, initText);
    new Watcher(this.$vm, exp, function (value) {
      self.updateText(node, value);
    });
  },

  updateText: function (node, value) {
    node.textContent = typeof value == "undefined" ? "" : value;
  },
};

處理解析指令對(duì)相關(guān)指令進(jìn)行函數(shù)綁定。

Compile.prototype = {

  ......
  isDirective: function(attr) {
    return attr.indexOf('v-') == 0;
  },

  isEventDirective: function(dir) {
    return dir.indexOf('on:') === 0;
  },

  //處理v-指令
  compile: function(node) {

    var nodeAttrs = node.attributes,
      self = this;
    [].slice.call(nodeAttrs).forEach(function(attr) {
      // 規(guī)定:指令以 v-xxx 命名
      // 如 <span v-text="content"></span> 中指令為 v-text
      var attrName = attr.name; // v-text
      if (self.isDirective(attrName)) {
        var exp = attr.value; // content
        var dir = attrName.substring(2); // text
        if (self.isEventDirective(dir)) {
          // 事件指令, 如 v-on:click
          self.compileEvent(node, self.$vm, exp, dir);
        } else {
          // 普通指令如:v-model, v-html, 當(dāng)前只處理v-model
          self.compileModel(node, self.$vm, exp, dir);
        }
        //處理完畢要干掉 v-on:, v-model 等元素屬性
        node.removeAttribute(attrName)
      }
    });

  },

  compileEvent: function(node, vm, exp, dir) {

    var eventType = dir.split(':')[1];
    var cb = vm.$options.methods && vm.$options.methods[exp];
    if (eventType && cb) {
      node.addEventListener(eventType, cb.bind(vm), false);
    }

  },

  compileModel: function(node, vm, exp, dir) {

    var self = this;
    var val = this.$vm[exp];
    this.updaterModel(node, val);
    new Watcher(this.$vm, exp, function(value) {
      self.updaterModel(node, value);
    });
    node.addEventListener('input', function(e) {
      var newValue = e.target.value;
      if (val === newValue) {
        return;
      }
      self.$vm[exp] = newValue;
      val = newValue;
    });

  },

  updaterModel: function(node, value, oldValue) {
    node.value = typeof value == 'undefined' ? '' : value;
  },

}

最后再關(guān)聯(lián)起來

function Vue(options) {
  .....
  observe(this.data, this);
  this.$compile = new Compile(options.el || document.body, this)
  return this;

}

來嘗試下效果

<!--html-->
<div id="app">
  <h3>{{name}}</h3>
  <input v-model="name" />
  <h2>{{name}}</h2>
  <button v-on:click="test">click here!</button>
</div>
<script>
  new Vue({
    el: "#app",
    data: {
      name: "chuchur",
      age: 29,
    },
    methods: {
      test() {
        this.name = "My name is chuchur";
      },
    },
  });
</script>

到此,關(guān)于“vue中complie數(shù)據(jù)雙向綁定原理”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

本文題目:vue中complie數(shù)據(jù)雙向綁定原理
瀏覽地址:http://chinadenli.net/article42/pddihc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、響應(yīng)式網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)關(guān)鍵詞優(yōu)化、企業(yè)建站、用戶體驗(yàn)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名