|
1 | 1 |
|
2 | 2 | import { createComponentInstance, setupComponent } from "./component"
|
3 |
| - |
| 3 | +import { isObject } from "../shared/index" |
4 | 4 | export function render(vnode, rootContainer) {
|
5 | 5 | console.log('===render执行挂载逻辑===');
|
6 | 6 | //执行挂载逻辑
|
7 |
| - path(vnode,rootContainer) |
| 7 | + path(vnode, rootContainer) |
8 | 8 | }
|
9 | 9 | //挂载逻辑
|
10 | 10 | function path(vnode, rootContainer) {
|
11 | 11 | //判断是不是DOM元素是DOM元素就处理DOM是组件就处理组件
|
12 |
| -console.log('===path根据type类型的不同来处理不同类型的vnode==='); |
| 12 | + console.log('===path根据type类型的不同来处理不同类型的vnode==='); |
13 | 13 |
|
14 | 14 | let { type } = vnode
|
15 | 15 | if (typeof type == "string") {
|
16 | 16 | //todo 处理 element 类型
|
17 | 17 | processElement(vnode, rootContainer)
|
18 |
| - } else if (typeof type ==="object") { |
| 18 | + } else if (isObject(type)) { |
19 | 19 | //todo 处理 component 类型
|
20 | 20 | processComponent(vnode, rootContainer)
|
21 | 21 | }
|
22 | 22 | }
|
23 | 23 | //处理DOM类型函数
|
24 | 24 | function processElement(vnode: any, rootContainer: any) {
|
| 25 | + // todo分为初始化过程和更新逻辑 |
25 | 26 | console.log("=== processElement 处理element 类型===");
|
26 |
| - |
27 |
| - mountElement(vnode,rootContainer) |
28 |
| - |
| 27 | + //初始化逻辑 |
| 28 | + mountElement(vnode, rootContainer) |
| 29 | + |
29 | 30 | }
|
30 | 31 | //挂载DOM
|
31 |
| -function mountElement(vnode:any,rootContainer:any) { |
32 |
| - let {type,props,children}=vnode; |
33 |
| - let divType=document.createElement(type); |
34 |
| - divType.innerHTML=children |
35 |
| - rootContainer.appendChild(divType) |
36 |
| - console.log("divType",divType,vnode,rootContainer); |
37 |
| - |
| 32 | +function mountElement(vnode: any, rootContainer: any) { |
| 33 | + let { type, props, children } = vnode; |
| 34 | + //todo children分两种情况 一种是string类型 一种是数组类型 |
| 35 | + let el = document.createElement(type); |
| 36 | + if (Array.isArray(children)) { |
| 37 | + //因为children里面每一个都是虚拟节点还需要调用path函数 |
| 38 | + mountChildren(children,el) |
| 39 | + } else { |
| 40 | + el.textContent = children; |
| 41 | + } |
| 42 | + //处理props |
| 43 | + for (const key in props) { |
| 44 | + //拿到props中的属性值 |
| 45 | + const val = props[key] |
| 46 | + //设置DOM元素的属性 |
| 47 | + el.setAttribute(key, val) |
| 48 | + } |
| 49 | + rootContainer.appendChild(el) |
| 50 | + console.log("divType", el, vnode, rootContainer); |
| 51 | + |
| 52 | +} |
| 53 | +//处理children |
| 54 | +function mountChildren(children,el){ |
| 55 | + children.forEach(v => { |
| 56 | + path(v, el) |
| 57 | + }) |
38 | 58 | }
|
39 | 59 | //处理组件函数
|
40 | 60 | function processComponent(vnode: any, rootContainer: any) {
|
41 |
| - console.log("===processComponent 处理组件类型函数我是组件对象====",vnode); |
| 61 | + console.log("===processComponent 处理组件类型函数我是组件对象====", vnode); |
42 | 62 | mountComponent(vnode, rootContainer)
|
43 | 63 | }
|
44 | 64 | //挂载组件
|
45 | 65 | function mountComponent(vnode: any, rootContainer: any) {
|
46 | 66 | //拿到组件实例
|
47 | 67 | const instance = createComponentInstance(vnode);
|
48 |
| - console.log("===拿到组件实例对象===",instance); |
49 |
| - |
| 68 | + console.log("===拿到组件实例对象===", instance); |
| 69 | + |
50 | 70 | //传入件实例 初始化组件
|
51 | 71 | setupComponent(instance);
|
52 | 72 | setupRenderEffect(instance, rootContainer)
|
|
0 commit comments