Observable<Data> 不存在 Angular 6.x Map 操作符使用 ActivatedRoute [英] Angular 6.x Map operator not exist for Observable<Data> with ActivatedRoute
问题描述
我最近遇到了一个问题,但没有找到有关如何使其工作的提示.当我使用 ActivatedRoute 从我的路由中获取数据时,angular 无法编译并说 Property 'map' 在类型 'Observable' 上不存在.
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';
甚至
import 'rxjs/add/operator/map';
但错误仍然存在.
这是我的路线路径:
{
path: 'home', component: HomeComponent, data: { title: "Accueil" }
},
{
path:'projets', component: ProjectsComponent, data: { title: "Projets"}
}
和组件代码:
constructor(private breakpointObserver: BreakpointObserver, private route: ActivatedRoute) {
}
ngOnInit() {
this.route.data.map( data => data.title).subscribe(title => console.log(title));
}
你知道现在需要什么吗?它在升级到 Angular 6 之前工作.
Do you have any idea what is required now ? It worked before upgrading to Angular 6.
预先感谢您的回答:)
推荐答案
Angular 6.x 使用 rxjs 6.x
.在rxjs6.x中,需要链接其他pipeable 运算符.
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));
这篇关于Observable<Data> 不存在 Angular 6.x Map 操作符使用 ActivatedRoute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!