Vue3屬性綁定方法解析
前言:
這篇文章來(lái)自我們團(tuán)隊(duì)的田鑫雨同學(xué),強(qiáng)勁的“后浪”。不論使用已有框架,還是自實(shí)現(xiàn)框架,數(shù)據(jù)綁定都是一個(gè)熱點(diǎn)話題,來(lái)看看他對(duì)Vue3數(shù)據(jù)綁定方式的分析
Vue3 通常使用 v-bind 綁定數(shù)據(jù)到子元素上,對(duì)于一個(gè)元素接收數(shù)據(jù)的方式有兩種:通過(guò)property或通過(guò)attribute,本文通過(guò)分析源碼得到結(jié)論:Vue會(huì)通過(guò)判斷元素實(shí)例el上是否有需要綁定的property,如果有就把數(shù)據(jù)傳給子元素的property,否則傳給attribute。當(dāng)然還有會(huì)一些特殊處理,我們這里只討論一般情況。
首先說(shuō)結(jié)論,對(duì)于一般的屬性,Vue會(huì)判斷元素el
上是否有對(duì)應(yīng)的property
屬性,如果有就賦值給對(duì)應(yīng)的property
,否則添加到attribute
上。然后Vue會(huì)對(duì)一些屬性做特殊處理直接綁定到attribute
,此外對(duì)input/textarea/select
元素也會(huì)有特殊處理。
Vue3.2版本還提供了
:xxx.prop
和:xxx.attr
寫(xiě)法指定綁定數(shù)據(jù)到property
或attribute
上
直接從源碼入手。
在Vue初始化過(guò)程中,Vue會(huì)將<template>
解析并構(gòu)造成vdom
,收集其中的數(shù)據(jù)綁定放在props
對(duì)象中。到了mount
階段Vue會(huì)根據(jù)vdom
創(chuàng)建為真實(shí)DOM,然后放到頁(yè)面中。
創(chuàng)建過(guò)程大致為遍歷vdom
中的vnode
節(jié)點(diǎn),執(zhí)行mountElement()
,關(guān)鍵代碼如下,根據(jù)vnode
創(chuàng)建真實(shí)el
元素,進(jìn)行數(shù)據(jù)綁定和事件綁定,然后把el
元素插入到父容器中,最后完成頁(yè)面的加載。
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); // ... };
這里以一個(gè)自定義的WebComponent元素
<te-tag>
為例
Vue使用createElement
創(chuàng)建了te-tag
元素對(duì)象保存在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); } };
通過(guò)源碼可以看到,除了class/style
的屬性,Vue會(huì)對(duì)我們自定義的屬性會(huì)進(jìn)行一個(gè)判斷, 對(duì)于key的值:
.xxx
表示通過(guò):xxx.prop
綁定的數(shù)據(jù),直接往property上設(shè)置^xxx
表示通過(guò):xxx.attr
綁定的值,應(yīng)該往attribute上設(shè)置- 不是以上兩種情況的key值如
xxx
,需要調(diào)用shouldSetAsProp()判斷是否應(yīng)該設(shè)置到property上
判斷為真綁定property
會(huì)執(zhí)行patchDOMProp()
,否則綁定attribute
會(huì)執(zhí)行pathAttr()
我們這里最關(guān)心的第三種情況執(zhí)行shouldSetAsProp()
來(lái)判斷是否應(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對(duì)SVG/spellcheck/draggale/translate/form/input[list]/textarea[type]/onclick
等做了特殊處理,要求返回false
綁定數(shù)據(jù)到attribute
上。
而我們自定義的屬性只通過(guò)一行代碼來(lái)判斷,
return key in el;
如果el
的property
上有key
,則返回true
,然后綁定數(shù)據(jù)到property
上。
到此這篇關(guān)于Vue3屬性綁定方法解析的文章就介紹到這了,更多相關(guān)Vue屬性綁定內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue+thinkphp5.1+axios實(shí)現(xiàn)文件上傳
這篇文章主要為大家詳細(xì)介紹了Vue+thinkphp5.1+axios實(shí)現(xiàn)文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05elementUI el-input 只能輸入正整數(shù)驗(yàn)證的操作方法
這篇文章主要介紹了elementUI el-input 只能輸入正整數(shù)驗(yàn)證,本文給大家詳細(xì)講解對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-11-11Vite項(xiàng)目搭建與環(huán)境配置的完整版教程
Vite?使用?Rollup?作為默認(rèn)的構(gòu)建工具,可以將應(yīng)用程序的源代碼打包成一個(gè)或多個(gè)優(yōu)化的靜態(tài)文件,本文就來(lái)為大家介紹一下Vite如何進(jìn)行項(xiàng)目搭建與環(huán)境配置吧2023-09-09Vue如何獲取元素高度總是不準(zhǔn)確的問(wèn)題
這篇文章主要介紹了Vue如何獲取元素高度總是不準(zhǔn)確的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10vue2.0項(xiàng)目實(shí)現(xiàn)路由跳轉(zhuǎn)的方法詳解
這篇文章主要介紹了vue2.0項(xiàng)目實(shí)現(xiàn)路由跳轉(zhuǎn)的詳細(xì)方法,非常不錯(cuò),具有一定的參考借鑒借鑒價(jià)值,需要的朋友可以參考下2018-06-06ajax請(qǐng)求+vue.js渲染+頁(yè)面加載的示例
下面小編就為大家分享一篇ajax請(qǐng)求+vue.js渲染+頁(yè)面加載的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02