Angular 6.x 具有 ActivatedRoute 的 Observable<Data> 不存在地图运算符
Angular 6.x Map operator not exist for Observable<Data> with ActivatedRoute
我最近遇到了一个问题,没有找到关于如何让它工作的提示。当我使用 ActivatedRoute 从我的路由获取数据时,angular 无法编译并显示 Property 'map' does not exist on type 'Observable<Data>'.
我尝试了所有方法:
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.
之前有效
提前感谢您的回答:)
Angular 6.x 使用 rxjs 6.x
。在 rxjs6.x 中,您需要在 Observable
的 pipe
运算符中链接其他 pipeable operators。
this.route.data
.pipe(
map( data => data.title)
)
.subscribe(title => console.log(title));
我最近遇到了一个问题,没有找到关于如何让它工作的提示。当我使用 ActivatedRoute 从我的路由获取数据时,angular 无法编译并显示 Property 'map' does not exist on type 'Observable<Data>'.
我尝试了所有方法:
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.
之前有效提前感谢您的回答:)
Angular 6.x 使用 rxjs 6.x
。在 rxjs6.x 中,您需要在 Observable
的 pipe
运算符中链接其他 pipeable operators。
this.route.data
.pipe(
map( data => data.title)
)
.subscribe(title => console.log(title));