3 Star 10 Fork 3

ifer/vue3_ts_77

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
test.html 1.20 KB
一键复制 编辑 原始数据 按行查看 历史
ifer 提交于 2022-08-08 18:02 +08:00 . init
<!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>
<p id="oP"></p>
<input type="text" id="oInput" />
<script>
// 观察者模式:可以观察到数据的变化,把变化之后的状态交给 watcher,watcher 调用 update 方法去做一些操作
class Dep {
constructor() {
// 里面装观察者
this.subs = []
this.state = 88888
}
addSub(sub) {
// 添加观察者
this.subs.push(sub)
}
notify(state) {
this.subs.forEach((sub) => sub.update(state))
}
setState(state) {
this.state = state
this.notify(state)
}
}
class Wather {
update(state) {
console.log('' + state + '治疗感冒')
}
}
const dep = new Dep()
const watcher = new Wather()
dep.addSub(watcher)
dep.addSub(watcher)
dep.setState(999)
</script>
</body>
</html>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ifercarly/vue3_ts_77.git
git@gitee.com:ifercarly/vue3_ts_77.git
ifercarly
vue3_ts_77
vue3_ts_77
master

搜索帮助