作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如 ofObjectChanges 建立在 Object.observe() 这是过时的( https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/observe )我正在寻找一种替代方法来观察对象属性的变化。有人知道吗?
最佳答案
也许使用代理是一种选择,尽管需要替换原始对象
const { Subject } = require('rxjs');
// Take an object, and return a proxy with an 'observation$' stream
const toObservableObject = targetObject => {
const observation$ = new Subject();
return new Proxy(targetObject, {
set: (target, name, value) => {
const oldValue = target[name];
const newValue = value;
target[name] = value;
observation$.next({ name, oldValue, newValue });
},
get: (target, name) => name == 'observation$' ? observation$ : target[name]
});
}
const observableObject = toObservableObject({ });
observableObject.observation$
.filter(modification => modification.name == 'something')
.subscribe(({ name, oldValue, newValue }) => console.log(`${name} changed from ${oldValue} to ${newValue}`));
observableObject.something = 1;
observableObject.something = 2;
something changed from undefined to 1
something changed from 1 to 2
关于rxjs ofObjectChanges 已过时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36001392/
如 ofObjectChanges 建立在 Object.observe() 这是过时的( https://developer.mozilla.org/en/docs/Web/JavaScript/R
我想创建类属性装饰器,它将查找此属性的所有更改并做一些事情。我怎样才能做到这一点? 在 RxJs 中我找到了 ofObjectChanges 方法 https://github.com/Reactiv
我是一名优秀的程序员,十分优秀!