Vue3屬性綁定方法解析
前言:
這篇文章來自我們團(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ù)到property
或attribute
上
直接從源碼入手。
在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;
如果el
的property
上有key
,則返回true
,然后綁定數(shù)據(jù)到property
上。
到此這篇關(guān)于Vue3屬性綁定方法解析的文章就介紹到這了,更多相關(guān)Vue屬性綁定內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue+thinkphp5.1+axios實現(xiàn)文件上傳
這篇文章主要為大家詳細(xì)介紹了Vue+thinkphp5.1+axios實現(xiàn)文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05elementUI el-input 只能輸入正整數(shù)驗證的操作方法
這篇文章主要介紹了elementUI el-input 只能輸入正整數(shù)驗證,本文給大家詳細(xì)講解對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-11-11vue2.0項目實現(xiàn)路由跳轉(zhuǎn)的方法詳解
這篇文章主要介紹了vue2.0項目實現(xiàn)路由跳轉(zhuǎn)的詳細(xì)方法,非常不錯,具有一定的參考借鑒借鑒價值,需要的朋友可以參考下2018-06-06