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

烈酒焚心 提交于 2019-12-02 12:15:54

问题


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 :)


回答1:


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));


来源:https://stackoverflow.com/questions/50340749/angular-6-x-map-operator-not-exist-for-observabledata-with-activatedroute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!