- 虛擬節點就是用一個對象來描述真實的
dom元素
源碼如下:
1 function $createElement(tag,data,...children){ 2 let key = data.key; 3 delete data.key; 4 children = children.map(child=>{ 5 if(typeof child === 'object'){ 6 return child 7 }else{ 8 return vnode(undefined,undefined,undefined,undefined,child) 9 } 10 }) 11 return vnode(tag,props,key,children); 12 } 13 export function vnode(tag,data,key,children,text){ 14 return { 15 tag, // 表示的是當前的標簽名 16 data, // 表示的是當前標簽上的屬性 17 key, // 唯一表示用戶可能傳遞 18 children, 19 text 20 } 21 }
