亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue3屬性綁定方法解析

 更新時間:2022年09月25日 09:54:33   作者:Web能力中心團(tuán)隊???????  
這篇文章主要介紹了Vue3屬性綁定方法解析,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下

前言:

這篇文章來自我們團(tuán)隊的田鑫雨同學(xué),強勁的“后浪”。不論使用已有框架,還是自實現(xiàn)框架,數(shù)據(jù)綁定都是一個熱點話題,來看看他對Vue3數(shù)據(jù)綁定方式的分析

Vue3 通常使用 v-bind 綁定數(shù)據(jù)到子元素上,對于一個元素接收數(shù)據(jù)的方式有兩種:通過property或通過attribute,本文通過分析源碼得到結(jié)論:Vue會通過判斷元素實例el上是否有需要綁定的property,如果有就把數(shù)據(jù)傳給子元素的property,否則傳給attribute。當(dāng)然還有會一些特殊處理,我們這里只討論一般情況。

首先說結(jié)論,對于一般的屬性,Vue會判斷元素el上是否有對應(yīng)的property屬性,如果有就賦值給對應(yīng)的property,否則添加到attribute上。然后Vue會對一些屬性做特殊處理直接綁定到attribute,此外對input/textarea/select元素也會有特殊處理。

Vue3.2版本還提供了:xxx.prop 和 :xxx.attr 寫法指定綁定數(shù)據(jù)到propertyattribute

直接從源碼入手。

在Vue初始化過程中,Vue會將<template>解析并構(gòu)造成vdom,收集其中的數(shù)據(jù)綁定放在props對象中。到了mount階段Vue會根據(jù)vdom創(chuàng)建為真實DOM,然后放到頁面中。

創(chuàng)建過程大致為遍歷vdom中的vnode節(jié)點,執(zhí)行mountElement(),關(guān)鍵代碼如下,根據(jù)vnode創(chuàng)建真實el元素,進(jìn)行數(shù)據(jù)綁定和事件綁定,然后把el元素插入到父容器中,最后完成頁面的加載。

const mountElement = (vnode, container, anchor, parentComponent, parentSuspense, isSVG, slotScopeIds, optimized) => {
  let el;
  // ...
  const { type, props, shapeFlag, transition, patchFlag, dirs } = vnode;
  {
      el = vnode.el = hostCreateElement(vnode.type, isSVG, props && props.is, props);
      // ...
      // props
      if (props) {
          for (const key in props) {
              if (key !== 'value' && !isReservedProp(key)) {
                  hostPatchProp(el, key, null, props[key], isSVG, vnode.children, parentComponent, parentSuspense, unmountChildren);
              }
          }
          // ...
      }
      // ...
  }
  // ...
  hostInsert(el, container, anchor);
  // ...
};

這里以一個自定義的WebComponent元素<te-tag>為例

Vue使用createElement創(chuàng)建了te-tag元素對象保存在el中,然后使用for (const key in props) {...}遍歷需要綁定的屬性,屬性名為key,屬性值為props[key],然后執(zhí)行hostPatchProp()將該屬性添加到el上。

hostPatchProp()patchProp()方法代碼如下

const patchProp = (el, key, prevValue, nextValue, isSVG = false, prevChildren, parentComponent, parentSuspense, unmountChildren) => {
      if (key === 'class') {
          patchClass(el, nextValue, isSVG);
      }
      else if (key === 'style') {
          patchStyle(el, prevValue, nextValue);
      }
      else if (isOn(key)) {
          // ignore v-model listeners
          if (!isModelListener(key)) {
              patchEvent(el, key, prevValue, nextValue, parentComponent);
          }
      }
      else if (key[0] === '.'
          ? ((key = key.slice(1)), true)
          : key[0] === '^'
              ? ((key = key.slice(1)), false)
              : shouldSetAsProp(el, key, nextValue, isSVG)) {
          patchDOMProp(el, key, nextValue, prevChildren, parentComponent, parentSuspense, unmountChildren);
      }
      else {
          if (key === 'true-value') {
              el._trueValue = nextValue;
          }
          else if (key === 'false-value') {
              el._falseValue = nextValue;
          }
          patchAttr(el, key, nextValue, isSVG);
      }
  };

通過源碼可以看到,除了class/style的屬性,Vue會對我們自定義的屬性會進(jìn)行一個判斷, 對于key的值:

  • .xxx表示通過:xxx.prop綁定的數(shù)據(jù),直接往property上設(shè)置
  • ^xxx表示通過:xxx.attr綁定的值,應(yīng)該往attribute上設(shè)置
  • 不是以上兩種情況的key值如xxx,需要調(diào)用shouldSetAsProp()判斷是否應(yīng)該設(shè)置到property上

判斷為真綁定property會執(zhí)行patchDOMProp(),否則綁定attribute會執(zhí)行pathAttr()

我們這里最關(guān)心的第三種情況執(zhí)行shouldSetAsProp()來判斷是否應(yīng)該把xxx設(shè)置到property上,其代碼如下

function shouldSetAsProp(el, key, value, isSVG) {
      if (isSVG) {
          // most keys must be set as attribute on svg elements to work
          // ...except innerHTML & textContent
          if (key === 'innerHTML' || key === 'textContent') {
              return true;
          }
          // or native onclick with function values
          if (key in el && nativeOnRE.test(key) && isFunction(value)) {
              return true;
          }
          return false;
      }
      // these are enumerated attrs, however their corresponding DOM properties
      // are actually booleans - this leads to setting it with a string "false"
      // value leading it to be coerced to `true`, so we need to always treat
      // them as attributes.
      // Note that `contentEditable` doesn't have this problem: its DOM
      // property is also enumerated string values.
      if (key === 'spellcheck' || key === 'draggable' || key === 'translate') {
          return false;
      }
      // #1787, #2840 form property on form elements is readonly and must be set as
      // attribute.
      if (key === 'form') {
          return false;
      }
      // #1526 <input list> must be set as attribute
      if (key === 'list' && el.tagName === 'INPUT') {
          return false;
      }
      // #2766 <textarea type> must be set as attribute
      if (key === 'type' && el.tagName === 'TEXTAREA') {
          return false;
      }
      // native onclick with string value, must be set as attribute
      if (nativeOnRE.test(key) && isString(value)) {
          return false;
      }
      return key in el;
  }

這里可以看到Vue對SVG/spellcheck/draggale/translate/form/input[list]/textarea[type]/onclick等做了特殊處理,要求返回false綁定數(shù)據(jù)到attribute上。

而我們自定義的屬性只通過一行代碼來判斷,

return key in el;

如果elproperty上有key,則返回true,然后綁定數(shù)據(jù)到property上。

到此這篇關(guān)于Vue3屬性綁定方法解析的文章就介紹到這了,更多相關(guān)Vue屬性綁定內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解Vue?自定義hook?函數(shù)

    詳解Vue?自定義hook?函數(shù)

    這篇文章主要介紹了詳解Vue自定義hook函數(shù),hook函數(shù)本質(zhì)是一個函數(shù),把setup函數(shù)中使用的Composition?API進(jìn)行了封裝,更多相關(guān)內(nèi)容感興趣的朋友可以參考一下
    2022-06-06
  • Vue+thinkphp5.1+axios實現(xiàn)文件上傳

    Vue+thinkphp5.1+axios實現(xiàn)文件上傳

    這篇文章主要為大家詳細(xì)介紹了Vue+thinkphp5.1+axios實現(xiàn)文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • vue在組件中使用v-model的場景

    vue在組件中使用v-model的場景

    這篇文章主要介紹了vue在組件中使用v-model的場景,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-07-07
  • elementUI el-input 只能輸入正整數(shù)驗證的操作方法

    elementUI el-input 只能輸入正整數(shù)驗證的操作方法

    這篇文章主要介紹了elementUI el-input 只能輸入正整數(shù)驗證,本文給大家詳細(xì)講解對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-11-11
  • 從0開始學(xué)Vue

    從0開始學(xué)Vue

    從零開始學(xué)Vue,通過一些例子,讓大家概覽一些基本的概念和特性,理解Vue的基礎(chǔ)知識,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Vite項目搭建與環(huán)境配置的完整版教程

    Vite項目搭建與環(huán)境配置的完整版教程

    Vite?使用?Rollup?作為默認(rèn)的構(gòu)建工具,可以將應(yīng)用程序的源代碼打包成一個或多個優(yōu)化的靜態(tài)文件,本文就來為大家介紹一下Vite如何進(jìn)行項目搭建與環(huán)境配置吧
    2023-09-09
  • Vue中axios攔截器如何單獨配置token

    Vue中axios攔截器如何單獨配置token

    這篇文章主要介紹了Vue axios攔截器如何單獨配置token及vue axios攔截器的使用,需要的朋友可以參考下
    2019-12-12
  • Vue如何獲取元素高度總是不準(zhǔn)確的問題

    Vue如何獲取元素高度總是不準(zhǔn)確的問題

    這篇文章主要介紹了Vue如何獲取元素高度總是不準(zhǔn)確的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue2.0項目實現(xiàn)路由跳轉(zhuǎn)的方法詳解

    vue2.0項目實現(xiàn)路由跳轉(zhuǎn)的方法詳解

    這篇文章主要介紹了vue2.0項目實現(xiàn)路由跳轉(zhuǎn)的詳細(xì)方法,非常不錯,具有一定的參考借鑒借鑒價值,需要的朋友可以參考下
    2018-06-06
  • ajax請求+vue.js渲染+頁面加載的示例

    ajax請求+vue.js渲染+頁面加載的示例

    下面小編就為大家分享一篇ajax請求+vue.js渲染+頁面加載的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02

最新評論