Search code examples
angularangular-router

Angular 6.x Map operator not exist for Observable<Data> with ActivatedRoute


I had a problem recently, and found no tips on how to get it work. When I use ActivatedRoute in order to get data from my Routes, angular failed to compile and say Property 'map' does not exist on type 'Observable<Data>'.

I tried everything like :

import { map } from "rxjs/operators";
import 'rxjs/operators';

or even

import 'rxjs/add/operator/map'; 

but the error persists.

Here is the route path that I have :

{
    path: 'home', component: HomeComponent, data: { title: "Accueil" }
},
{
    path:'projets', component: ProjectsComponent, data: { title: "Projets"}
}

and the component code:

constructor(private breakpointObserver: BreakpointObserver, private route: ActivatedRoute) {
}
ngOnInit() {
    this.route.data.map( data => data.title).subscribe(title => console.log(title));
}

Do you have any idea what is required now ? It worked before upgrading to Angular 6.

Thanks in advance for your answer :)


Solution

  • Angular 6.x uses rxjs 6.x. In rxjs6.x, you need to chain other pipeable operators in Observable's pipe operator.

    this.route.data
              .pipe(
                 map( data => data.title)
                )
              .subscribe(title => console.log(title));