Angular 6 router.events.filter 'filter' does not exist on type 'Observable'

后端 未结 3 2174
猫巷女王i
猫巷女王i 2020-12-25 10:20

I have finished to update my App to Angular 6 (it was in 5.2 version).

I got an error syntax in :

import { Router, ActivatedRoute, NavigationEnd } fr         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-25 10:52

    This is how to filter router events with Angular 6+ and latest RxJS:

    import { Component, OnInit } from '@angular/core';
    import { Router, ActivatedRoute, NavigationEnd } from '@angular/router';
    
    import { filter } from 'rxjs/operators';
    
    export class MyComponent implements OnInit {
        constructor(private router: Router, private activatedRoute: ActivatedRoute) {}
    
        ngOnInit() {
            this.router.events.pipe(
                filter(event => event instanceof NavigationEnd)
            ).subscribe(() => {
                console.log(this.activatedRoute.root);
            });
        }
    }
    

    Uses the pipe operator instead of attempting to chain filter on the observable.

提交回复
热议问题