gpt4 book ai didi

Angular 7 - 重新加载/刷新数据不同的组件

转载 作者:行者123 更新时间:2023-12-04 15:48:45 26 4
gpt4 key购买 nike

如何在组件 2 发生更改时刷新不同组件 1 中的数据。这两个组件不在同一父节点下。

customer.service.ts

export class UserManagementService extends RestService {

private BASE_URL_UM: string = '/portal/admin';

private headers = new HttpHeaders({
'Authorization': localStorage.getItem('token'),
'Content-Type': 'application/json'
});

constructor(protected injector: Injector,
protected httpClient: HttpClient) {
super(injector);
}
getEapGroupList(): Observable < EapGroupInterface > {
return this.get < GroupInterface >
(this.getFullUrl(`${this.BASE_URL_UM}/groups`), {
headers: this.headers
});
}

updateGroup(body: CreateGroupPayload): Observable < CreateGroupPayload > {
return this.put < GroupPayload >
(this.getFullUrl(`${this.BASE_URL_UM}/group`), body, {
headers: this.headers
});
}
}

Component1.ts

export class UserGroupComponent implements OnInit {

constructor(private userManagementService: UserManagementService) {}

ngOnInit() {
this.loadGroup();
}

loadGroup() {
this.userManagementService.getEapGroupList()
.subscribe(response => {
this.groups = response;
})
}

}
<mat-list-item *ngFor="let group of groups?.groupList" role="listitem">
<div matLine [routerLink]="['/portal/user-management/group', group.groupCode, 'overview']" [routerLinkActive]="['is-active']">
{{group.groupName}}
</div>
</mat-list-item>
<mat-sidenav-content>
<router-outlet></router-outlet>
</mat-sidenav-content>

component2.ts

setPayload() {
const formValue = this.newGroupForm.value
return {
'id': '5c47b24918a17c0001aa7df4',
'groupName': formValue.groupName,
}
}

onUpdateGroup() {
this.userManagementService.updateGroup(this.setPayload())
.subscribe(() => {
console.log('success);
})
}

当我在 component1 中更新 onUpdateGroup() api 时,loadGroup() 应该在 component2 中刷新

最佳答案

将检索数据的代码移至服务,以便服务维护

然后将组件 1 中的数据包装在一个 getter 中:

get groups() {
return this.userManagementService.groups
}

然后每次数据发生变化时,Angular 的依赖注入(inject)会自动调用 getter 并获取最新的值。

修改后的服务

export class UserManagementService extends RestService {
groups;
private BASE_URL_UM: string = '/portal/admin';

private headers = new HttpHeaders({
'Authorization': localStorage.getItem('token'),
'Content-Type': 'application/json'
});

constructor(protected injector: Injector,
protected httpClient: HttpClient) {
super(injector);

// Get the data here in the service
this.loadGroup();
}

getEapGroupList(): Observable < EapGroupInterface > {
return this.get < GroupInterface >
(this.getFullUrl(`${this.BASE_URL_UM}/groups`), {
headers: this.headers
});
}

loadGroup() {
this.getEapGroupList()
.subscribe(response => {
this.groups = response;
})
}

updateGroup(body: CreateGroupPayload): Observable < CreateGroupPayload > {
return this.put < GroupPayload >
(this.getFullUrl(`${this.BASE_URL_UM}/group`), body, {
headers: this.headers
}).pipe(
// Reget the data after the update
tap(() => this.loadGroup()
);
}
}

修订后的组件 1

export class UserGroupComponent implements OnInit {
get groups() {
return this.userManagementService.groups
}

constructor(private userManagementService: UserManagementService) {}

ngOnInit() {

}
}

注意:此代码未经过语法检查!

我这里有一个类似的工作示例:https://github.com/DeborahK/Angular-Communication/tree/master/APM-FinalWithGetters

(检查 product-shell 文件夹文件和 product.service.ts)

关于Angular 7 - 重新加载/刷新数据不同的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54759713/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com