Skip to content

Instantly share code, notes, and snippets.

@suukii
Last active September 17, 2020 09:05
Show Gist options
  • Save suukii/679ae4bfe41a1590bc8d885b5a05a88f to your computer and use it in GitHub Desktop.
Save suukii/679ae4bfe41a1590bc8d885b5a05a88f to your computer and use it in GitHub Desktop.
从零实现一个 Mini Vue
<!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