欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺析vue 函數(shù)配置項watch及函數(shù) $watch 源碼分享

 更新時間:2018年11月22日 14:34:12   作者:moderiona  
這篇文章主要介紹了vue 函數(shù)配置項watch及函數(shù) $watch 源碼分享 ,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下

Vue雙向榜單的原理

大家都知道Vue采用的是MVVM的設(shè)計模式,采用數(shù)據(jù)驅(qū)動實現(xiàn)雙向綁定,不明白雙向綁定原理的需要先補充雙向綁定的知識,在watch的處理中將運用到Vue的雙向榜單原理,所以再次回顧一下:

Vue的數(shù)據(jù)通過Object.defineProperty設(shè)置對象的get和set實現(xiàn)對象屬性的獲取,vue的data下的數(shù)據(jù)對應(yīng)唯一 一個dep對象,dep對象會存儲改屬性對應(yīng)的watcher,在獲取數(shù)據(jù)(get)的時候為相關(guān)屬性添加具有對應(yīng)處理函數(shù)的watcher,在設(shè)置屬性的時候,觸發(fā)def對象下watcher執(zhí)行相關(guān)的邏輯

// 為data的的所有屬性添加getter 和 setter
function defineReactive( obj,key,val,customSetter,shallow
) {
  //
  var dep = new Dep();
  /*....省略部分....*/
  var childOb = !shallow && observe(val); //為對象添加備份依賴dep
  Object.defineProperty(obj, key, {
    enumerable: true,
    configurable: true,
    get: function reactiveGetter() {
      var value = getter ? getter.call(obj) : val;
      if (Dep.target) {
        dep.depend(); // 
        if (childOb) {
          childOb.dep.depend(); //依賴dep 添加watcher 用于set ,array改變等使用
          if (Array.isArray(value)) {
            dependArray(value);
          }
        }
      }
      return value
    },
    set: function reactiveSetter(newVal) {
      var value = getter ? getter.call(obj) : val;
      /* eslint-disable no-self-compare */
      if (newVal === value || (newVal !== newVal && value !== value)) {
        return
      }
      /* eslint-enable no-self-compare */
      if ("development" !== 'production' && customSetter) {
        customSetter();
      }
      if (setter) {
        setter.call(obj, newVal);
      } else {
        val = newVal;
      }
      childOb = !shallow && observe(newVal);
      dep.notify();//有改變觸發(fā)watcher進行更新
    }
  });
} 

在vue進行實例化的時候,將調(diào)用 initWatch(vm, opts.watch);進行初始化watch的初始化,該函數(shù)最終將調(diào)用 vm.$watch(expOrFn, handler, options) 進行watch的配置,下面我們將講解 vm.$watch方法

Vue.prototype.$watch = function (
      expOrFn,
      cb,
      options
    ) {
      var vm = this;
      if (isPlainObject(cb)) {
        return createWatcher(vm, expOrFn, cb, options)
      }
      options = options || {};
      options.user = true;
      //為需要觀察的 expOrFn 添加watcher ,expOrFn的值有改變時執(zhí)行cb,
      //在watcher的實例化的過程中會對expOrFn進行解析,并為expOrFn涉及到的data數(shù)據(jù)下的def添加該watcher
      var watcher = new Watcher(vm, expOrFn, cb, options);
      //immediate==true 立即執(zhí)行watch handler
      if (options.immediate) { 
        cb.call(vm, watcher.value);
      }
      //取消觀察函數(shù)
      return function unwatchFn() {
        watcher.teardown();
      }
    };

來看看實例化watcher的過程中(只分享是觀察函數(shù)中的實例的watcher)

var Watcher = function Watcher(
    vm,
    expOrFn,
    cb,
    options,
    isRenderWatcher
  ) {
    this.vm = vm;
    if (isRenderWatcher) {
      vm._watcher = this;
    }
    vm._watchers.push(this);
    // options
    if (options) {
      this.deep = !!options.deep; //是否觀察對象內(nèi)部值的變化
      this.user = !!options.user;
      this.lazy = !!options.lazy;
      this.sync = !!options.sync;
    } else {
      this.deep = this.user = this.lazy = this.sync = false;
    }
    this.cb = cb; // 觀察屬性改變時執(zhí)行的函數(shù)
    this.id = ++uid$1; // uid for batching
    this.active = true;
    this.dirty = this.lazy; // for lazy watchers
    this.deps = [];
    this.newDeps = [];
    this.depIds = new _Set();
    this.newDepIds = new _Set();
    this.expression = expOrFn.toString();
    // parse expression for getter
    if (typeof expOrFn === 'function') {
      this.getter = expOrFn;
    } else {
      // 將需要觀察的數(shù)據(jù):string | Function | Object | Array等進行解析 如:a.b.c, 并返回訪問該表達式的函數(shù) 
      this.getter = parsePath(expOrFn); 
      if (!this.getter) {
        this.getter = function () { };
        "development" !== 'production' && warn(
          "Failed watching path: \"" + expOrFn + "\" " +
          'Watcher only accepts simple dot-delimited paths. ' +
          'For full control, use a function instead.',
          vm
        );
      }
    }
    // this.get()將訪問需要觀察的數(shù)據(jù) 
    this.value = this.lazy
      ? undefined
      : this.get(); 
  };
  /**
   * Evaluate the getter, and re-collect dependencies.
   */
  Watcher.prototype.get = function get() {
    //this為$watch方法中實例化的watcher
    pushTarget(this);講this賦給Dep.target并緩存之前的watcher
    var value;
    var vm = this.vm;
    try {
      //訪問需要觀察的數(shù)據(jù),在獲取數(shù)據(jù)的getter中執(zhí)行dep.depend();將$watch方法中實例化的watcher添加到對應(yīng)數(shù)據(jù)下的dep中
      value = this.getter.call(vm, vm); 
    } catch (e) {
      if (this.user) {
        handleError(e, vm, ("getter for watcher \"" + (this.expression) + "\""));
      } else {
        throw e
      }
    } finally {
      // "touch" every property so they are all tracked as
      // dependencies for deep watching
      if (this.deep) {
        traverse(value);
      }
      popTarget(); //將之前的watcher賦給Dep.target
      this.cleanupDeps();
    }
    return value
  };<br><br><br><br> 
  Watcher.prototype.run = function run() {

/*....省略部分....*/ 
    var value = this.get(); //重新獲取info的值
    var oldValue = this.value; //保存老的值
    this.value = value;
    this.cb.call(this.vm, value, oldValue); //執(zhí)行watch的回調(diào)
/*....省略部分....*/ 
  };

  以上代碼在watcher實例化的時候執(zhí)行  this.getter = parsePath(expOrFn); 返回一個訪問該屬性的函數(shù),參數(shù)為被訪問的對象  如vm.$watch("info",function(new, old){console.log("watch success")});, this.getter =function(obj){return obj.info};,在執(zhí)行watcher的get方法中,將執(zhí)行value = this.getter.call(vm, vm);,觸發(fā)屬性的get方法,添加該watcher至info屬性對應(yīng)的def對象中,如果需要深度監(jiān)聽,將執(zhí)行traverse(value),依次訪問info(假設(shè)info只對象)對象下的屬性,如果info的屬性還有是對象的屬性,將進行遞歸訪問,以達到info以及info下所有的屬性的def對象都會添加該watcher實例。

     當(dāng)我們執(zhí)行vm.info="change"時,將出發(fā)info的set方法,執(zhí)行dep.notify();出發(fā)info所依賴的watcher執(zhí)行watcher的run方法,即實現(xiàn)監(jiān)聽

總結(jié)

以上所述是小編給大家介紹的vue 函數(shù)配置項watch及函數(shù) $watch 源碼分享,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Vue3?攜手?TypeScript?搭建完整項目結(jié)構(gòu)

    Vue3?攜手?TypeScript?搭建完整項目結(jié)構(gòu)

    TypeScript 是JS的一個超級,主要提供了類型系統(tǒng)和對ES6的支持,使用 TypeScript 可以增加代碼的可讀性和可維護性,在 react 和 vue 社區(qū)中也越來越多人開始使用TypeScript,這篇文章主要介紹了Vue3?攜手?TypeScript?搭建完整項目結(jié)構(gòu),需要的朋友可以參考下
    2022-04-04
  • vue-cli-service不是內(nèi)部或外部命令,也不是可運行的程序或批處理文件問題

    vue-cli-service不是內(nèi)部或外部命令,也不是可運行的程序或批處理文件問題

    在Vue項目構(gòu)建過程中,如果遇到無法識別'vue-cli-service'命令的錯誤提示,通常是因為沒有全局安裝vue-cli,解決這個問題的步驟主要包括:首先檢查Vue版本,如果未安裝則先安裝Vue;其次全局安裝vue-cli;若在安裝過程中遇到cnpm命令找不到的情況
    2024-10-10
  • vue3?使用setup語法糖實現(xiàn)分類管理功能

    vue3?使用setup語法糖實現(xiàn)分類管理功能

    這篇文章主要介紹了vue3?使用setup語法糖實現(xiàn)分類管理,本次模塊使用 vue3+element-plus 實現(xiàn)一個新聞?wù)镜暮笈_分類管理模塊,其中新增、編輯采用對話框方式公用一個表單,需要的朋友可以參考下
    2022-08-08
  • Vue多系統(tǒng)切換實現(xiàn)方案

    Vue多系統(tǒng)切換實現(xiàn)方案

    本篇文章給大家分享了關(guān)于Vue多系統(tǒng)切換實現(xiàn)的解決方案,對此有需要的朋友可以跟著參考學(xué)習(xí)下。
    2018-06-06
  • 徹底搞懂Transition內(nèi)置組件

    徹底搞懂Transition內(nèi)置組件

    這篇文章主要為大家介紹了Transition內(nèi)置組件使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • vue操作下拉選擇器獲取選擇的數(shù)據(jù)的id方法

    vue操作下拉選擇器獲取選擇的數(shù)據(jù)的id方法

    今天小編就為大家分享一篇vue操作下拉選擇器獲取選擇的數(shù)據(jù)的id方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • 通過vue-cli來學(xué)習(xí)修改Webpack多環(huán)境配置和發(fā)布問題

    通過vue-cli來學(xué)習(xí)修改Webpack多環(huán)境配置和發(fā)布問題

    這篇文章主要介紹了隨著Vue-cli來'學(xué)'并'改'Webpack之多環(huán)境配置和發(fā)布的相關(guān)知識,本文將會根據(jù)一些實際的業(yè)務(wù)需求,先學(xué)習(xí)vue-cli生成的模版,然后在進行相關(guān)修改,感興趣的朋友一起跟著小編學(xué)習(xí)吧
    2017-12-12
  • vue二級菜單導(dǎo)航點擊選中事件的方法

    vue二級菜單導(dǎo)航點擊選中事件的方法

    今天小編就為大家分享一篇vue二級菜單導(dǎo)航點擊選中事件的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • vue實現(xiàn)畫筆回放canvas轉(zhuǎn)視頻播放功能

    vue實現(xiàn)畫筆回放canvas轉(zhuǎn)視頻播放功能

    這篇文章主要介紹了vue實現(xiàn)畫筆回放,canvas轉(zhuǎn)視頻播放功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2024-01-01
  • vue3 學(xué)習(xí)筆記之a(chǎn)xios的使用變化總結(jié)

    vue3 學(xué)習(xí)筆記之a(chǎn)xios的使用變化總結(jié)

    本篇文章主要旨在幫助正在學(xué)vue3或者準備學(xué)vue3的同學(xué)了解網(wǎng)絡(luò)請求axios該如何使用,防止接觸了一點點vue3的同學(xué)會有個疑問。有興趣的小伙伴可以關(guān)注一下
    2021-11-11

最新評論