在服务中创建一个或多个主题的可观察值?

发布于 2025-01-10 11:18:36 字数 637 浏览 5 评论 0原文

根据所咨询的来源/站点,我们可以看到两种创建具有主题/可观察量的服务以共享数据的方法:

  1. 初始化主题的可观察量并返回它(主题的一个可观察量)
export class TodoService {

    private _todo = new BehaviorSubject<any>([]);
    readonly todos$ = this._todo.asObservable();

    constructor() {}

    getTodos$() {
        return this.todos$;
    }
}
  1. 创建主题的可观察量每次我们想要获取数据(主题的多个可观察值)时,
export class TodoService {

    private _todo = new BehaviorSubject<any>([]);

    constructor() {}

    getTodos$() {
        return this._todo.asObservable();
    }
}

应该采用这两种方法中的哪一种?它们之间有什么区别?

谢谢

According to the sources/sites consulted, we can see two approaches of creating a service with a subject/observable in order to share data :

  1. Initializing an observable of the subject and returning it (ONE observable of the subject)
export class TodoService {

    private _todo = new BehaviorSubject<any>([]);
    readonly todos$ = this._todo.asObservable();

    constructor() {}

    getTodos$() {
        return this.todos$;
    }
}
  1. Creating an observable of the subject each time we want to get the data (MULTIPLE observables of the subject)
export class TodoService {

    private _todo = new BehaviorSubject<any>([]);

    constructor() {}

    getTodos$() {
        return this._todo.asObservable();
    }
}

Which of those two approaches should be adopted ? What is the difference between them ?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唯憾梦倾城 2025-01-17 11:18:36

两者之间的唯一区别是第一个只会有一个与主题链接的可观察量实例,而第二个每次都会创建一个新的可观察量。不应该有任何实际差异。

我会使用两种替代方法之一,具体取决于隐藏主题是否真的很重要。

仅主题

通过隐藏主题可以实现什么目的?如果从外部调用 .next() 真的会造成任何伤害吗?除非确实有充分的理由隐藏它,否则我会将主题设为只读并让消费者直接订阅。对于我创建的专门在内部使用的任何服务,我对此抱有偏见。

readonly todoSubject = new BehaviorSubject<any>([]);

可观察字段

如果必须隐藏主题,则只需为可观察对象使用只读字段。为什么必须从方法中检索 todos$?除非该方法涉及一些设置,否则没有。这与您的第一个示例基本相同,但没有该方法。

private _todo = new BehaviorSubject<any>([]);
readonly todos$ = this._todo.asObservable();

The only difference between the two is the first will only have one instance of an observable linked to the subject, whereas the second will create a new observable each time. There shouldn't be any practical differences.

I would use one of two alternative approaches depending on if it's actually important to hide the subject.

Subject Only

What are you accomplishing by hiding the subject? Does it really hurt anything if .next() is called externally? Unless there really is a good reason to hide it, I would make the subject read only and let consumers subscribe directly. I am biased towards this for any services I create that are exclusively used internally.

readonly todoSubject = new BehaviorSubject<any>([]);

Observable Field

If the subject must be hidden, then just use a read only field for the observable. Why do the todos$ have to be retrieved from a method? Unless there's some setup involved in there method, there is none. This is basically the same as your first example without the method.

private _todo = new BehaviorSubject<any>([]);
readonly todos$ = this._todo.asObservable();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文