After introducing how to create Signals and how to update them, let’s take a look at one more exciting feature that helps replace the need for RxJs Observables.
How to emit a new Signal value when one or more Signals get updated? That’s what computed()
does. In my Signals course, I illustrate computed()
with the following example:
In the above code, this.rates()
and this.currency()
are two different Signals. this.rates()
emits up-to-date exchange rates for all currencies in the world. this.currency()
emits the current currency selected by the user.
computed()
takes a function as a parameter. The function returns the computed value from my two Signals; in this case, the up-to-date exchange rate for the current currency. If the exchange rates or the currency get updated, this computed Signal will emit an updated value automatically.
This is somewhat similar to combining several Observables and using switchMap
or combineLatest
to get a customized result. It’s a lot easier with Signals (one line of code!).