最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue?data中隨意改一個(gè)屬性,視圖都會更新嗎?

 更新時(shí)間:2021年12月15日 11:42:43   作者:Rudy24  
這篇文章主要討論Vue?data中隨意改一個(gè)屬性,視圖都會更新嗎?下面來自面試官的問題然后做i出的一個(gè)問題總結(jié),具有一定的參考價(jià)值,需要的小伙伴可以參考一下
  • 面試官:看過 Vue 的源碼沒?
  • 候選者:看過。
  • 面試官:那你說下 Vue data 中隨意更改一個(gè)屬性,視圖都會被更新嗎?
  • 候選者:不會。
  • 面試官:why?
  • 候選者:如果該屬性沒有被用到 template 中,就沒有必要去更新視圖,頻繁這樣性能不好。
  • 面試官:那 Vue 中是如何去實(shí)現(xiàn)該方案的?
  • 候選者:在實(shí)例初始化過程中,利用Object.defineProperty對 data 中的屬性進(jìn)行數(shù)據(jù)監(jiān)聽,如果在 template 中被使用到的屬性,就被 Dep 類收集起來,等到屬性被更改時(shí)會調(diào)用notify更新視圖。
  • 面試官:那你怎么知道那些屬性是在 template 被用到的呢?
  • 候選者:WTF。。。這個(gè)倒不是很清楚,您能解釋下嗎?
  • 面試官:OK,那我就簡單解釋下:

先寫個(gè)簡單的 demo,其中 data 中有 4 個(gè)屬性a,b,c,d,在模板中被利用到的屬性只有a,b??纯词遣皇侵挥衋,b才會調(diào)用Dep收集起來呢?

new Vue({
  el: '#app',
  data() {
    return {
      a: 1,
      b: 2,
      c: 3,
      d: 4,
    };
  },
  created() {
    console.log(this.b);
    this.b = 'aaa';
  },
  template: '<div>Hello World{{a}}{}</div>',
});


在Vueinstance/state.js里面,會利用proxy把每個(gè)屬性都 代理一遍

const keys = Object.keys(data)
  const props = vm.$options.props
  const methods = vm.$options.methods
  let i = keys.length
  while (i--) {
    const key = keys[i]
    if (props && hasOwn(props, key)) {
      process.env.NODE_ENV !== 'production' && warn(
        `The data property "${key}" is already declared as a prop. ` +
        `Use prop default value instead.`,
        vm
      )
    } else if (!isReserved(key)) {
      // 代理對象的屬性
      proxy(vm, `_data`, key)
    }
  }
  // observe data
  observe(data, true /* asRootData */)


利用defineReactive對data中的每個(gè)屬性進(jìn)行劫持

observe(data, true /* asRootData */);

// observe
const keys = Object.keys(obj);
for (let i = 0; i < keys.length; i++) {
  defineReactive(obj, keys[i]);
}

// defineReactive
Object.defineProperty(obj, key, {
  enumerable: true,
  configurable: true,
  get: function reactiveGetter() {
    const value = getter ? getter.call(obj) : val;
    // 重點(diǎn)在這里,后續(xù)如果在模板中使用到的屬性,都會被執(zhí)行reactiveGetter函數(shù)
    // 被Dep類 收集起來
    if (Dep.target) {
      console.log(`${key} 屬性 被Dep類收集了`)
      dep.depend();
      if (childOb) {
        childOb.dep.depend();
        if (Array.isArray(value)) {
          dependArray(value);
        }
      }
    }
    return value;
  },
  set: function reactiveSetter(newVal) {
    const value = getter ? getter.call(obj) : val;
    /* eslint-disable no-self-compare */
    if (newVal === value || (newVal !== newVal && value !== value)) {
      return;
    }
    if (setter) {
      // 這里是處理computed set 函數(shù)
      setter.call(obj, newVal);
    } else {
      val = newVal;
    }
    childOb = !shallow && observe(newVal);
    // 如果我們在更改屬性時(shí),就會調(diào)用notify 異步更新視圖
    dep.notify();
  },
});

執(zhí)行$mount進(jìn)行視圖掛載

if (vm.$options.el) {
  vm.$mount(vm.$options.el);
}


$mount 是調(diào)用 Vue 原型上的方法, 重點(diǎn)是最后一句 mount.call(this, el, hydrating)

Vue.prototype.$mount = function (
  el?: string | Element,
  hydrating?: boolean
): Component {
  el = el && query(el);

  const options = this.$options;
  // resolve template/el and convert to render function
  /**
   * 查看render 函數(shù)是否存在?如果不存在就解析template模板
   * Vue渲染頁面時(shí),有兩個(gè)方式 1. template,2. render,最終所有的模板類的都需要使用render去渲染
   */
  if (!options.render) {
    let template = options.template;
    if (template) {
      if (typeof template === 'string') {
        if (template.charAt(0) === '#') {
          template = idToTemplate(template);
          /* istanbul ignore if */
          if (process.env.NODE_ENV !== 'production' && !template) {
            warn(
              `Template element not found or is empty: ${options.template}`,
              this
            );
          }
        }
      } else if (template.nodeType) {
        template = template.innerHTML;
      } else {
        if (process.env.NODE_ENV !== 'production') {
          warn('invalid template option:' + template, this);
        }
        return this;
      }
    } else if (el) {
      // 如果模板不存在,就創(chuàng)建一個(gè)默認(rèn)的html模板
      template = getOuterHTML(el);
    }
  }
  // 重寫了Vue.prototype.$mount ,最終調(diào)用緩存的mount方法完成對$mount的掛載
  return mount.call(this, el, hydrating);
};

這里mount調(diào)用了 mountComponent(this, el, hydrating) 方法,而 mountComponent是執(zhí)行了 _render函數(shù),最終_render是調(diào)用render 生成一個(gè)vnode。

const { render, _parentVnode } = vm.$options;
vnode = render.call(vm._renderProxy, vm.$createElement);


最后一張圖可以看到是render函數(shù)在渲染我們demo里面的template模板,最終只有a, b兩個(gè)屬性才會被Dep類收集起來。

如果文中有錯(cuò)誤的地方,麻煩各位指出,我會持續(xù)改進(jìn)的。謝謝, 需要調(diào)試源碼的,這里點(diǎn)擊這里,按照 readme操作即可。希望star下

到此這篇關(guān)于Vue data中隨意改一個(gè)屬性,視圖都會更新?的文章就介紹到這了,更多相關(guān)Vue data 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

永安市| 兴国县| 尉犁县| 鄂伦春自治旗| 东明县| 米易县| 通州市| 饶河县| 海伦市| 库尔勒市| 鄂伦春自治旗| 通河县| 泾川县| 福清市| 秀山| 平凉市| 韩城市| 攀枝花市| 桂阳县| 芜湖县| 彭水| 无为县| 琼中| 获嘉县| 芮城县| 新蔡县| 黔东| 华池县| 晋中市| 佛冈县| 温泉县| 双辽市| 邛崃市| 定西市| 木里| 龙泉市| 柳州市| 横山县| 漳州市| 万年县| 光山县|