We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Simple Vue</title> </head> <style> #app { text-align: center; } </style> <body> <div id="app"> <p id="vdom">初始化中</p> </div> </body> <script> // 被监听的数据 let data = { message: "" }; // 虚拟dom集合 let nodes = []; // 设置虚拟dom let el = document.querySelector("#app"); const fragment = document.createDocumentFragment(); let child = document.querySelector("#vdom");; fragment.appendChild(child) nodes.push(fragment.childNodes[0]) el.appendChild(fragment) // 设置监听 let watcher = { update() { // 更新dom nodes[0].textContent = data.message } }; // 监听和分发集合 let dep = { subs: [], notify() { this.subs.forEach(item => { item.update(); }) } } // 设置监听数据的getter和setter Object.defineProperty(data, 'message', { enumerable: true, configurable: true, get() { return this.value; }, set(newVal) { // 如果值一样,不更新dom if (newVal === this.value) { return; } this.value = newVal; // 触发所有监听 dep.notify(); } }); // 添加监听到集合中 dep.subs.push(watcher) // 定时更新数据,更新dom setInterval(() => { data.message = "当前时间戳为:" + Date.now() }, 1000); </script> </html>
The text was updated successfully, but these errors were encountered:
No branches or pull requests
知识点
The text was updated successfully, but these errors were encountered: