angular - 使用 rxjs Observable 订阅 mobx @observable 但未在深层组件(ts 文件)中进行更改
问题描述
我正在尝试使用 rxjs observable 从 angular-mobx 商店获取更改。但如果观察到的数组发生变化,则不会从那里得到任何变化。但是,如果我使用“=”符号分配新值,那么我会在订阅中得到更改。任何人都可以解释吗?或仅通过更改(例如拼接或替换数组中的对象)来帮助更改?谢谢
https://stackblitz.com/edit/angular-reuych DEMO APP
import { computed, action, observable } from "mobx-angular";
import {observe } from "mobx";
import { Observable } from 'rxjs';
import { Injectable } from "@angular/core";
import * as moment from "moment-timezone";
@Injectable()
export class Store {toRx(obj, prop) {
return Observable.create(observer =>
observe(obj, prop, (change) => observer.next(change.newValue), true)
);
}
@observable storeCampaigns:any=[];
}
然后在像这样订阅的组件中
this.store.toRx(this.store.storeCampaigns, 'campaigns')
.subscribe(val => {
console.log("calendar get change", val)
解决方案
如果您深入了解,mobx
您将到达以下链接观察者,您可以在其中找到以下评论
/**
* A node in the state dependency root that observes other nodes, and can be observed itself.
*
* ComputedValue will remember the result of the computation for the duration of the batch, or
* while being observed.
*
* During this time it will recompute only when one of its direct dependencies changed,
* but only when it is being accessed with `ComputedValue.get()`.
*
* Implementation description:
* 1. First time it's being accessed it will compute and remember result
* give back remembered result until 2. happens
* 2. First time any deep dependency change, propagate POSSIBLY_STALE to all observers, wait for 3.
* 3. When it's being accessed, recompute if any shallow dependency changed.
* if result changed: propagate STALE to all observers, that were POSSIBLY_STALE from the last step.
* go to step 2. either way
*
* If at any point it's outside batch and it isn't observed: reset everything and go to 1.
*/
这几乎表明只有在第一次访问该值(即订阅时)或依赖项发生更改时(即当您更改存储的参考资料,在您的情况下与=
商店内的操作员一起使用)。
因此,如果我必须对其工作方式进行总结,则其行为与其他状态管理库非常相似,它们仅在商店中的某些 ref 发生更改时才向其订阅者推送新值。
** 免责声明,不是 mobx pro,但这就是源代码所说的。
推荐阅读
- database - DeltaLake:如何在数据集之间进行无限时间旅行?
- azure-devops - Azure DevOps 直接访问代理环境变量
- http - Multipart 和 MIME 内容类型有什么区别?
- terraform - 为什么我在 Terraform 中收到 random_id b64 属性的错误?
- mysql - mysql在使用二级索引时如何锁定我的数据?
- python - pip升级无法安装包
- spring-boot - 如何在 Service 类中只注入一个模拟并正常实例化到其他实例?
- c# - 填充无效,即使键相同也无法删除
- python - 将函数应用于数据框中的多个列并将结果存储在新列中
- angular - 打字稿或任何替代方案中的 LINQ