MiniVue
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app"></div>
<script src="../02-渲染器的实现/renderer.js"></script>
<script src="../03-响应式系统/reactive-01_vue3.js"></script>
<script src="./index.js"></script>
<script>
const App = {
data: reactive({
counter: 0
}),
render() {
return h('div', null, [
h('h2', null, `当前计数:${this.data.counter}`),
h('button', {
onClick: () => {
this.data.counter++
}
}, '+1')
])
}
}
createApp(App).mount('#app')
</script>
</body>
</html>
index.js
- 从框架的层面来说,我们需要有两部分内容:
createApp用于创建一个app对象;
- 该app对象有一个
mount方法,可以将根组件挂载到某一个dom元素上;
function createApp(rootComponent) {
return {
mount(Selector) {
const container = document.querySelector(Selector)
let isMounted = false
let oldVNode = null
watchEffect(() => {
if (!isMounted) {
oldVNode = rootComponent.render()
mounted(oldVNode, container)
isMounted = true
} else {
const newVNode = rootComponent.render()
console.log('newVNode', newVNode, oldVNode)
patch(oldVNode, newVNode)
oldVNode = newVNode
}
})
}
}
}
渲染器的实现
渲染器的实现
响应式系统
响应式系统