Skip to content

Commit dda7061

Browse files
author
liuss7
committed
实现初始化element主流程
1 parent d56d87c commit dda7061

File tree

6 files changed

+59
-25
lines changed

6 files changed

+59
-25
lines changed

example/Helloword/app.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ import {h} from "../../lib/mini-vue.esm.js"
22

33
export const App={
44
render(){
5-
return h('div',{color:'red'},'你好 mini-vue')
5+
return h(
6+
'div',
7+
{id:'idApp',class:['red','hard']},
8+
//string类型
9+
// '你好 mini-vue'
10+
//数组类型
11+
[h('p',{class:'red'},"hi 我是第个p"),h('p',{class:'blue'},'hi,我是第二个p')],
12+
)
613
},
714
setup(){
815
return {

example/Helloword/index.html

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
77
<title>Document</title>
88
</head>
9+
<style>
10+
.red{
11+
color: red;
12+
}
13+
.blue{
14+
color: blue;
15+
}
16+
</style>
917
<body>
1018

1119
<div id="app"></div>

rollup.config.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11

22
import typescript from "@rollup/plugin-typescript"
3-
3+
import pkg from "./package.json"
44
export default {
55
input:'./src/index.ts',
66
output:[
77
{
88
format:"cjs",
9-
file:'lib/mini-vue.cjs.js'
9+
file:"lib/mini-vue.cjs.js"
1010
},
1111
{
1212
format:"es",
13-
file:'lib/mini-vue.esm.js'
13+
file:"lib/mini-vue.esm.js"
1414
}
1515
],
1616
plugins:[

src/runtime-core/component.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function createComponentInstance(vnode: any) {
66
type: vnode.type
77
}
88
console.log("===返回组件实例===",component);
9-
return component
9+
return component;
1010
}
1111
export function setupComponent(instance) {
1212
console.log("===初始化组件实例对象===",instance);
@@ -40,7 +40,7 @@ function handleSetupResult(instance: any, setupResult: any) {
4040
function finishComponentSetup(instance: any) {
4141
console.log("====finishComponentSetup=====");
4242

43-
const component = instance.type
44-
instance.render = component.render
43+
const component = instance.type;
44+
instance.render = component.render;
4545
}
4646

src/runtime-core/h.ts

-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ import { createVNode } from "./vnode"
22
export function h(type: any,props?: any,children?: any){
33
return createVNode(type,props,children)
44
}
5-
//ceshi

src/runtime-core/render.ts

+37-17
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,72 @@
11

22
import { createComponentInstance, setupComponent } from "./component"
3-
3+
import { isObject } from "../shared/index"
44
export function render(vnode, rootContainer) {
55
console.log('===render执行挂载逻辑===');
66
//执行挂载逻辑
7-
path(vnode,rootContainer)
7+
path(vnode, rootContainer)
88
}
99
//挂载逻辑
1010
function path(vnode, rootContainer) {
1111
//判断是不是DOM元素是DOM元素就处理DOM是组件就处理组件
12-
console.log('===path根据type类型的不同来处理不同类型的vnode===');
12+
console.log('===path根据type类型的不同来处理不同类型的vnode===');
1313

1414
let { type } = vnode
1515
if (typeof type == "string") {
1616
//todo 处理 element 类型
1717
processElement(vnode, rootContainer)
18-
} else if (typeof type ==="object") {
18+
} else if (isObject(type)) {
1919
//todo 处理 component 类型
2020
processComponent(vnode, rootContainer)
2121
}
2222
}
2323
//处理DOM类型函数
2424
function processElement(vnode: any, rootContainer: any) {
25+
// todo分为初始化过程和更新逻辑
2526
console.log("=== processElement 处理element 类型===");
26-
27-
mountElement(vnode,rootContainer)
28-
27+
//初始化逻辑
28+
mountElement(vnode, rootContainer)
29+
2930
}
3031
//挂载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+
})
3858
}
3959
//处理组件函数
4060
function processComponent(vnode: any, rootContainer: any) {
41-
console.log("===processComponent 处理组件类型函数我是组件对象====",vnode);
61+
console.log("===processComponent 处理组件类型函数我是组件对象====", vnode);
4262
mountComponent(vnode, rootContainer)
4363
}
4464
//挂载组件
4565
function mountComponent(vnode: any, rootContainer: any) {
4666
//拿到组件实例
4767
const instance = createComponentInstance(vnode);
48-
console.log("===拿到组件实例对象===",instance);
49-
68+
console.log("===拿到组件实例对象===", instance);
69+
5070
//传入件实例 初始化组件
5171
setupComponent(instance);
5272
setupRenderEffect(instance, rootContainer)

0 commit comments

Comments
 (0)