您在这里的行为主题以及它对您的作用有点混淆。
主题实际上是您可以订阅的某个来源的值流。每当发出新值时,它都会到达您的 subscribe 方法。
如果你有初始状态,你可以使用行为主体来初始化主体并确保新订阅者总是收到一个值。
如果您没有初始状态,但想确保新订阅者在订阅时获得最后发出的值(如果存在),那么您可以使用重播主题。
进入一个主题的所有值都是类型 T,你有Subject<T>。所以在你的情况下,进入主题的所有内容都应该是AddressDto。
如果你有一个初始地址,那么你可以这样设置你的行为主题:
// somehow get my initial address
const address = new AddressDto(); //
const test = new BehaviorSubject<AddressDto>(address);
// all subscribers will receive this address upon subscribing
// ... some things happen
// now I have another address, emit that
const newAddress = new AddressDto();
test.next(newAddress);
// all new subscribers will now receive newAddress upon subscribing
另一方面,如果您没有初始地址,则可以使用这样的重播主题:
// always emit the last address to new subscribers by intitialing with a 1
// New subscribers won't receive an address until one is emitted
const test = new ReplaySubject<AddressDto>(1);
// ... some things happen
// now I have my first address, emit that
const firstAddress = new AddressDto();
test.next(firstAddress);
// all current subscribers receive firstAddress
// all future subscribers will receive firstAddress upon subscribing
// ... some things happen
const secondAddress = new AddressDto();
test.next(secondAddress);
// all current subscribers receive secondAddress
// all future subscribers will now receive secondAddress upon subscribing
编辑:
您已经询问了将最后一个值存储在变量中的问题。你的意思是模棱两可的,所以我假设你的意思是源头,因为那更复杂。
一旦您了解了主题/可观察的概念,您就会开始了解可观察管道的概念。各种各样的事情都可能发生在管道内——只要把它想象成一系列可能发生在一个对象上的事情的步骤,就像一个普通的 javascript 数组上的一系列链式数组函数。
您可以在管道中做的一件事是在tap() 运算符中执行“副作用”。这只是意味着您可以在管道中间做一些事情,同时让数据通过。其中之一可能是出于某种目的将值存储在变量(或本地存储或其他)中。
如果您可以控制主题中的内容,那么在管道中执行此操作似乎是多余的,因此我将使用缓存 http 请求结果的示例。
this.http.get(url).pipe(
// transform the http response into an object you have created
map(response => this.mapResponseToMyClass(response)),
// store the mapped object in a local property for later use
tap(myClass => {
// you can perform any side-effect actions you want here
console.log(myClass);
// store the value in a variable
this.cachedMyClass = myClass;
})
);
为您自己的主题提供管道也没有什么不同 - 进入主题的所有内容都将通过管道,然后传递给订阅者。
private subject = new Subject<AddressDto>();
getPostcode(): Observable<string> {
// reuse the local subject. All subscribers to this function will receive addresses that have come through the pipe.
return subject.pipe(
map(address => address.postcode),
// store the last postcode in a local property
tap(postcode => this.lastPostcode = postcode)
// the postcode still comes out here to all subscribers
).asObservable();
}