迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > Angular >

Angular 中的 BehaviorSubject 与 Observable

作者:迹忆客 最近更新:2023/03/23 浏览次数:

Angular BehaviorSubject 是一个主体,它发出源 Observable 发出的最后一个值。相反,observable 用于随时间发出值。

Angular 使用它们来处理事件或数据流,例如 HTTP 请求或用户输入。当需要通知观察者对象的状态更改时,通常也会使用它们。

详细来说,让我们讨论一下 BehaviorSubjectObservable 之间的区别。


Angular 中下 BehaviorSubjectObservable 的主要区别

BehaviorSubject 是具有定义特征的 Angular observable;它理论上是 Observable 的子类型; Observable 是通用的。在 BehaviorSubject 的帮助下,可以使用一个主题来构造一个可观察的对象。

主要区别在于你不能使用 next() 方法将数据传递给 observable。

让我们讨论 Angular ObservableBehaviorSubject 的基本概念。


Angular 中的 Observable

在 Angular 应用程序中,RxJS 提供 observables; observables 促进了作者和用户之间的数据共享。

Observable 是事件管理、异步计算和管理多个属性的更好策略。

Observables 的一个独特方面是它们只能由订阅它们的用户处理,就像订阅的消费者没有实现组件一样。用户只能在订阅之前接收更新。


Angular 中的 BehaviorSubject 是什么

通常,该主题以三种方式实现,每种方式提供不同的能力并且可以应用于各种应用。

  1. ReplaySubject:此对象跟踪所有已推送的值。它将源发出的所有项目分发给所有订阅它的观察者。
  2. BehaviorSubject:它是存储当前值的那个。订阅 BehaviorSubject 的观察者将随着时间的推移收到一个值。
  3. AsyncSubject:它保存最新的值并在序列完成时传输它。

Angular 中的 BehaviorSubject 允许你向 Observable 发送和检索值。例如,访问控制状态是一个 BehaviorSubject,它提供了一个不断变化的值。

在这种情况下,BehaviorSubject 的主要目标是确保每个用户都收到最终值。

因为它必须始终在订阅时提供一个值,即使它没有获取下一个值,行为主题也需要一个起始值。它在订阅时返回主题的最新值。

getValue() 方法可用于随时访问主体在不可观察代码中的最后一个值。

此外,向 BehaviorSubject 函数 Object() 提供参数以反映初始状态。当建立一个状态的需求并进行任何进一步的订阅时,BehaviorSubject 将首先提供最新的状态。


Angular 中的 BehaviorSubject 示例

为了更好地理解上面提到的讨论,让我们讨论一个例子。

App.component.ts

import { Component } from "@angular/core";
import { BehaviorSubject } from "rxjs";
interface Directory {
  name: string;
  id: string;
}
@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent {
  name: string;
  id: string;

  directories: BehaviorSubject<Directory[]> = new BehaviorSubject<Directory[]>(null);
  constructor() {
    this.directories.next([
    ]);
  }
  add() {
    const currentItems = this.directories.getValue();
    currentItems.push({ name: this.name, id: this.id });
  }
  delete() {
    const currentItems = this.directories.getValue();
    const itemsWithoutDeleted = currentItems.filter(d => d.id !== this.id);
    this.directories.next(itemsWithoutDeleted);
  }
}

App.component.html

<h1>Example of Angular BehaviorSubject</h1>
<h3>Write anything in the box that you want to add in the directory list.</h3>
<ul>
	<li *ngFor="let directory of (directories | async)">
		{{ directory.name }}
	</li>
</ul>
<input type="text" [(ngModel)]="name" size="20" />
<br/>
<br/>
<button (click)="add()">Add</button>
<br/>
<br/>
<button (click)="delete()">Delete</button>
<hr />

BehaviorSubject 发出源 Observable 发出的最后一个值,如果你需要在特定时间范围内保持最新值,这会很有帮助。

当你需要了解数据集中的每一个变化时,可观察对象很有帮助,但在使用新值更新数据时不如 BehaviorSubject 高效。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便