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 组件时,我都需要解析更新的数据。
解决方案
您的解析器代码不正确。
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
将是未定义的。
推荐阅读
- android - 在 Flutter 中迭代未来地图
- xamarin.forms - 无法使带有 DryIoc RegisterServices 和平台特定支持的 Prism 8 工作
- javascript - 如何在应用浏览器的电报中使用电报 JS 标记内联键盘打开 URL?
- javascript - JavaScript 未捕获类型错误与数组中的对象
- python - 如何找到龟壳名称错误
- javascript - 使用 Web 组件在登录页面中保存凭据
- javascript - json discord webhook中的Javascript变量
- firebase - 如何仅授予对 Firebase 项目中一个应用的访问权限?
- python - 在 GPU 上运行 BERT SQUAD 模型
- html - angular 11 组件与服务器数据之间的链接问题