首页 > 解决方案 > 在 Angular 6 中使用解析器检索数据,但在对象中,数组为空。为什么?

问题描述

我有一个路径“用户/编辑”来编辑当前登录的用户。我有一个标签,我想编辑每个用户都有的“食谱”数组。我正在使用解析器获取有关用户的数据。我得到了他所有的食谱。问题是每个食谱都有一个“照片”数组,每个“食谱”的这个数组都是空的。我怎么能得到这些数据?

这是我的解析器

export class UserDetailResolver implements Resolve<User> {
constructor(private userService: UserService, private router: Router,
private authService: AuthService, private notif: NgxNotificationService ) 
{}

resolve(route: ActivatedRouteSnapshot): Observable<User> {
    return this.userService.getUser(this.authService.decodedToken.nameid).pipe(
        catchError(error => {
            this.notif.sendMessage('Problem retrieving data', 'warning', 'bottom-right');
            this.router.navigate(['/users']);
            return of(null);
        })
    );
}
}

用户编辑组件:

  export class UserEditComponent implements OnInit {
  user: User;
  recipes: Recipe[];
  constructor(private route: ActivatedRoute) { }

  ngOnInit() {
   this.route.data.subscribe(data => {
     this.user = data['user'];
     });

在我的应用程序的其他部分,我可以毫无问题地显示所有食谱及其照片,并制作了一个画廊,一切都很好。也许我应该使用第二个解析器来解析数组中的每个配方,但我能做到吗?

标签: asp.net-mvcangular6entity-framework-coreef-core-2.1angular-resolver

解决方案


我找到了答案,问题出在 api 中。我没有在回复中包含我的照片。如果管理员决定删除问题或锁定它。


推荐阅读