Resolver: export const homeDataFetchGuard = (route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Response<Array<DisplayAnime>>> => { return inject(AnimeService).getUpdAniAnimeList(); };
http 服务:
getUpdAniAnimeList() {
return this.httpClient.get<Response<Array<DisplayAnime>>> (${environment.apiUrl}/anime/updAnimeList
);
}
路由模块: const routes: Routes = [{ path: '', component: MainComponent, children:[ { path: '', component: HomeComponent, resolve: { data: homeDataFetchGuard }}, ] }, { path: '**', redirectTo: '' } ];
@NgModule({ imports: [RouterModule.forRoot(routes, { scrollPositionRestoration: 'top' })], exports: [RouterModule] }) export class AppRoutingModule { }
component:
constructor(rivate route: ActivatedRoute) { }
ngOnInit(): void { this.route.data.pipe(map(data => data as Response<Anime>)).subscribe(res => { console.log(res.code); //里面的数据为 undefined });
}
大哥们帮忙看看为什么触发这个订阅时 res 内部的数据都为 undefined ,如果代码不够详细我可以提供更多,问题棘手的话我可以付费