Last active
September 17, 2020 09:05
-
-
Save suukii/679ae4bfe41a1590bc8d885b5a05a88f to your computer and use it in GitHub Desktop.
从零实现一个 Mini Vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Mini Vue</title> | |
</head> | |
<body> | |
<div id="counter"></div> | |
<button id="inc">inc</button> | |
<script> | |
const incBtn = $('#inc'); | |
const counterContainer = $('#counter'); | |
incBtn.addEventListener('click', () => { | |
counter.count++; | |
}); | |
const counter = { | |
count: 1, | |
}; | |
const counterComponent = { | |
render(state) { | |
return h('h1', {}, String(state.count)); | |
}, | |
}; | |
observe(counter); | |
let oldNode = null; | |
autorun(function () { | |
if (oldNode) { | |
const newNode = counterComponent.render(counter); | |
patch(oldNode, newNode); | |
oldNode = newNode; | |
} else { | |
oldNode = counterComponent.render(counter); | |
mount(oldNode, counterContainer); | |
} | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment