【问题标题】:load data in child component using resolver after data updated in the component在组件中更新数据后使用解析器在子组件中加载数据
【发布时间】:2020-09-28 02:56:33
【问题描述】:

我有一个 Angular 组件“update_profile”,它使用解析器服务加载数据。

我还使用选择器在另一个组件“complete_profile”中调用“update_profile”。当需要编辑时,我正在通过 ngIf 启动组件。

为了能够在“complete_profile”中加载数据,我还在父组件中注入了解析器。

const routes: Routes = [
  {
    path: "",
    component: OrganiserLayoutComponent,
    children: [
      { 
        path: "complete_profile", 
        component: MyProfileComponent, 
        resolve: { profileDetails: UpdateProfileResolverService } 
      },
      {
        path: "update_profile",
        component: UpdateProfileComponent,
        resolve: { profileDetails: UpdateProfileResolverService },
      },
    ],
  },
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
})

update_profile 组件

ngOnInit() {
    this.dataSubscription = this.activatedRoute.data.subscribe(
    (data: { profileDetails: any }) => {
      this.profileData = data.profileDetails["data"];
      console.log(this.profileData);
      },
    );
}

解析服务

import { Injectable } from '@angular/core';
import { Resolve, RouterStateSnapshot, ActivatedRouteSnapshot } from '@angular/router';
import { AppServiceService } from './../../../common/services/app-service.service';
import { AuthServiceService } from './../../../common/services/auth-service.service';

@Injectable({providedIn:'root'})
export class UpdateProfileResolverService implements Resolve<any> {
  constructor(public appService: AppServiceService, public auService: AuthServiceService) {}

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot){
    let currentUser;
    this.auService.User.subscribe(user=>{
      if(user){
        currentUser=user.token;
      }
    }).unsubscribe();
    return this.appService.getParams2('/organizer/', currentUser.Id);
  }
}

在 complete_profile 中调用 update_profile

<app-update-profile *ngIf="isShown"></app-update-profile>

当我要去 update_profile 路线时,我的数据总是会更新.. 没关系。 但是在 complete_profile 组件中,update_profile 是通过在 update_profile 选择器上应用 ngIf 指令来显示的,并且每当我单击按钮给出真实条件以在 complete_profile 组件中显示 update_profile 组件时,数据都不会更新。它显示了解析器在完成配置文件初始化时检索到的先前数据。

我了解解析器仅在路由更改时才解析数据。但每当 update_profile 组件初始化时,我都需要解析更新的数据。

【问题讨论】:

  • 您可以使用complete_profile和update_profile组件之间的父chilc关系在组件之间发送和接收数据。
  • 我没有将数据从一个组件传递到另一个组件。事实上,我将整个组件展示为另一个组件的一部分。我面临的问题是在我在更新完整配置文件组件内的 update_profile 组件中更新数据后关闭组件后,当我重新打开 update_profile 组件时,未从解析器收到更新的数据。

标签: angular angular-router angular-resolver


【解决方案1】:

您的解析器代码不正确。

import { Injectable } from '@angular/core';
import { Resolve, RouterStateSnapshot, ActivatedRouteSnapshot } from '@angular/router';
import { AppServiceService } from './../../../common/services/app-service.service';
import { AuthServiceService } from './../../../common/services/auth-service.service';
import { take, map } from 'rxjs/operators'

@Injectable({providedIn:'root'})
export class UpdateProfileResolverService implements Resolve<any> {
  constructor(public appService: AppServiceService, public auService: AuthServiceService) {}

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot){

    return this.auService.User.pipe( 
      take(1),
      map(user => this.appService.getParams2('/organizer/', user?.Id);
    )
  }
}

本质上,您需要在订阅块内返回对this.appService.getParams2 的调用,否则您为currentUser 拥有的变量将是未定义的。

【讨论】:

  • 其实这不是问题,currentUser 是可用的,因此没有问题。我想要的是,从路由解析器获取数据,而不刷新页面。我得到了一个代码let currentUrl=this.router.url; this.router.navigateByUrl('/', { skipLocationChange: true }).then(() =&gt; this.router.navigate([currentUrl]) ); 这种sn-p 的作品,但会导致页面的小幅刷新。你有什么更好的形式吗?
  • 解析器中的resolve 函数仅在您更改路由时执行。你想返回什么数据?是this.appService.getParams2('/organizer/', user?.Id) 吗?如果您希望在不重新路由的情况下刷新页面上的信息,那么使用解析器是错误的。您可能想要使用服务。
猜你喜欢
  • 2019-05-24
  • 2018-06-22
  • 1970-01-01
  • 2019-01-21
  • 1970-01-01
  • 2019-02-19
  • 2018-03-25
  • 2019-04-08
  • 2019-12-09
相关资源
最近更新 更多