//1.创建虚拟节点,将虚拟节点转化为真实节点
//2.组件的实现 setup
//3.reactive api实现effect
//4.diff算法
//5.vite
let { render} = Vue
const state = {
count:0
const vnode = {
tag:'div',
props:{color:'red'},
children:[
tag:'p',
props:{color:'blue},
children:[
'vue@3-计数器'
tag:'p',
props:{
onClick:()=>{
alert(state.count)
children:[
'vue@3-计数器'
render(vnode,app)
export function render(vnode,container){
// 渲染页面的方法叫patch
//1.第一次渲染 2.dom-diff
patch(null,vnode,container)
* n1 老的虚拟节点
* n2 新的虚拟节点
* container 容器
function patch(n1,n2,container){
//组件的虚拟节点是一个对象,tag是一个对象
//如果是组件,tag可能是个对象
//后续diff可以执行这个方法
if(typeof n2.tag ==='string'){
mountElement(n2,container)
}else if(typeof n2.tag==='object'){
function mountElement(vnode,container){
const { tag,children,props } = vnode
//虚拟节点和真实节点做映射关系
let el = (vnode.el = nodeOps.createElement(tag))
if(Array.isArray(children)){
mountChild(children,el)
}else{
nodeOps.hostSetElementText(el,children)
container.insert(el,container)
function mountChild(children,container){
for(var i=0;i<children.length;i++){
let child = children[i]
patch(null,child,container)
//节点操作方法
exoprt const nodeOps = {
//插入元素节点
insert(child,parent,anchor){
if(anchor){
parent.insertBefore(child,anchor)
}else{
parent.appendChild(child)
//移除节点
remove(child){
const parent = child.parentNode;
parent && parent.removeChild(child)
//创建节点
createElement(tag){
return document.createElement(tag)
//设置文本内容
hostSetElementText(el,text){
el.textContent = text
Vue3.x快速上手
Vue3.x已成为稳定版本,也就意味着Vue3.x将比Vue2.x更多的在后续的项目中被使用,最近也开始学习Vue3.x,感觉好像比别人学的晚了一点,不过没有关系,现在开始学习永远是最好的时间。