Angular Rxjs

Observables

Observable
Observer
Subject
subscriber – subscribe()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 每个猎人有自己的等级, 可以发布寻求帮助任务, 也可以订阅别人的寻求帮助任务
function Hunter(name, level) {
this.name = name;
this.level = level;
this.task = [];
}

Hunter.prototype.publish = function (money) {
console.log(this.level + "猎人" + this.name + '寻求帮助');
this.list.forEach( (item, index) => item(money))
}

Hunter.prototype.subscribe = function (target, fn) {
console.log(this.level + "猎人" + this.name + '订阅任务');
target.list.push(fn)
}

let HunterZhang = new Hunter('小张', 'level1');
let HunterMing = new Hunter('小明', 'level5');
let HunterWang = new Hunter('小王', 'level2');
let HunterLi = new Hunter('小李', 'level3');

// 小张等级低,需要发布寻求帮助


// 其他猎人,订阅小张的发布请求


本文作者: 孟 虎
本文链接: https://menghu1994.github.io/blog/2023/10/Angular/Observables%20&%20RxJS/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!