在Ionic中,通常使用Resolver来从API中获取数据并在加载组件之前将其解析。如果在组件中使用Resolver,那么你需要确保正确地访问上下文数据。如果你的组件中使用的数据未定义,请尝试从resolver中获取上下文数据,如下所示:
在你的resolver中:
resolve(route: ActivatedRouteSnapshot) {
return this.dataService.getData().pipe(
map(res => ({ data: res }))
);
}
在你的组件中:
constructor(
private route: ActivatedRoute,
) {}
ngOnInit() {
this.route.data.subscribe(res => console.log(res));
}
这将在控制台中打印你的数据,以确保你可以将其正确地访问到。另外,确保在你的组件模板中正确地引用数据,如下所示:
{{item.name}}