Hi I am using Angular 2 final with router 3.0. I want to filter the events that are emitted from this.router.events
What I want to do :
import 'rxjs/operator/filter';
//...
this.router.events
.filter((event:Event) => event instanceof NavigationEnd)
.subscribe(x => console.log(x))
event
can be instanceOf NavigationEnd
, NavigationStart
or RoutesRecognized
but I want only NavigationEnd
. But I get an error that
Property 'filter' does not exist on type Observable<Event>
during compile time.
When i import the whole rxjs
library the error disappears. What should I import to make it work without loading the full rxjs library ?
UPDATE
For RXJS 5.x
version:
import 'rxjs/add/operator/filter';
For RXJS 6.x
version:
import { filter } from 'rxjs/operators';
The following rules have been designed by the RxJS team to help JavaScript developers refactor import paths:
rxjs/operators
: Contains all pipeable operators.
import { map, filter, scan } from 'rxjs/operators';
rxjs
: Contains creation methods, types, schedulers, and utilities.
import { Observable, Subject, asapScheduler, pipe, of, from, interval, merge, fromEvent } from 'rxjs';
There are several possible fixes for this scenario.
Pipeable operators are meant to be a better approach for pulling in just the operators you need than the "patch" operators found in rxjs/add/operator/*
import { filter } from 'rxjs/operators';
// ..
this.router.events.pipe(
filter((event:Event) => event instanceof NavigationEnd)
).subscribe(x => console.log(x))
Change the import statement to import 'rxjs/add/operator/filter'
. This will modify Observable.prototype
and add filter
method to an every instance of the Observable class.
There are two consequences:
filter()
method will magically appear under Observable
while using the library)The statement import 'rxjs/operator/filter'
is perfectly valid. It will import just the operator. This approach will not mess with the Observable.prototype
. On downside it will make it more difficult to chain several operators.
import 'rxjs/operator/filter'; // This is valid import statement.
// It will import the operator without
// modifying Observable prototype
// ..
// Change how the operator is called
filter.call(
this.router.events,
(event:Event) => event instanceof NavigationEnd
).subscribe(x => console.log(x));
More details: Pipeable Operators
Angular Update(5.x to 6.x) also comes with update of rxjs from 5.x to 6.x So simply add
import { filter } from 'rxjs/operators';
then
this.router.events.pipe(
filter((event:Event) => event instanceof NavigationEnd)
).subscribe(x => console.log(x))
Hope That helps someone
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With