Vue3屬性綁定方法解析
前言:
這篇文章來自我們團隊的田鑫雨同學,強勁的“后浪”。不論使用已有框架,還是自實現(xiàn)框架,數(shù)據(jù)綁定都是一個熱點話題,來看看他對Vue3數(shù)據(jù)綁定方式的分析
Vue3 通常使用 v-bind 綁定數(shù)據(jù)到子元素上,對于一個元素接收數(shù)據(jù)的方式有兩種:通過property或通過attribute,本文通過分析源碼得到結論:Vue會通過判斷元素實例el上是否有需要綁定的property,如果有就把數(shù)據(jù)傳給子元素的property,否則傳給attribute。當然還有會一些特殊處理,我們這里只討論一般情況。
首先說結論,對于一般的屬性,Vue會判斷元素el上是否有對應的property屬性,如果有就賦值給對應的property,否則添加到attribute上。然后Vue會對一些屬性做特殊處理直接綁定到attribute,此外對input/textarea/select元素也會有特殊處理。
Vue3.2版本還提供了
:xxx.prop和:xxx.attr寫法指定綁定數(shù)據(jù)到property或attribute上
直接從源碼入手。
在Vue初始化過程中,Vue會將<template>解析并構造成vdom,收集其中的數(shù)據(jù)綁定放在props對象中。到了mount階段Vue會根據(jù)vdom創(chuàng)建為真實DOM,然后放到頁面中。
創(chuàng)建過程大致為遍歷vdom中的vnode節(jié)點,執(zhí)行mountElement(),關鍵代碼如下,根據(jù)vnode創(chuàng)建真實el元素,進行數(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會對我們自定義的屬性會進行一個判斷, 對于key的值:
.xxx表示通過:xxx.prop綁定的數(shù)據(jù),直接往property上設置^xxx表示通過:xxx.attr綁定的值,應該往attribute上設置- 不是以上兩種情況的key值如
xxx,需要調用shouldSetAsProp()判斷是否應該設置到property上
判斷為真綁定property會執(zhí)行patchDOMProp(),否則綁定attribute會執(zhí)行pathAttr()
我們這里最關心的第三種情況執(zhí)行shouldSetAsProp()來判斷是否應該把xxx設置到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上。
到此這篇關于Vue3屬性綁定方法解析的文章就介紹到這了,更多相關Vue屬性綁定內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue+thinkphp5.1+axios實現(xiàn)文件上傳
這篇文章主要為大家詳細介紹了Vue+thinkphp5.1+axios實現(xiàn)文件上傳,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05
elementUI el-input 只能輸入正整數(shù)驗證的操作方法
這篇文章主要介紹了elementUI el-input 只能輸入正整數(shù)驗證,本文給大家詳細講解對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-11-11

