在 Spartacus 中为客户检索当前 B2B 单位
Retrieve current B2B Unit for Customer in Spartacus
我想在 header 中显示当前的 B2B 单位名称。
为当前客户检索 B2B 单元的正确方法是什么?
到目前为止,我发现/current 有一个负载?在初始页面加载时加载的端点,它包含 orgUnit:
当我尝试通过 CurrentUnitService 检索单位时,我得到了空值。
unit.component.ts:
orgUnit$: Observable<B2BUnit> = this.currentUnitService.item$;
constructor(
protected currentUnitService: CurrentUnitService,
) {}
unit.component.html:
<ng-container *ngIf="orgUnit$ | async as unit">
<p class="d-inline-block">{{unit.name}} </p>
<a href="#" class="d-inline-block">
({{unit.uid}})
</a>
</ng-container>
我能够使用 UserAccountFacade 并通过使用 orgUnit 扩展用户模型来检索当前客户的 B2BUnit,如下所示:
unit.component.ts
export class UnitComponent implements OnInit {
user$: Observable<CustomUser | undefined>;
constructor(
protected auth: AuthService,
protected userAccount: UserAccountFacade
) {}
ngOnInit(): void {
this.user$ = this.auth.isUserLoggedIn().pipe(
switchMap(isUserLoggedIn => {
if (isUserLoggedIn) {
return this.userAccount.get();
} else {
return of(undefined);
}
})
);
}
}
interface CustomUser extends User {
orgUnit?: B2BUnit
}
unit.component.html
<ng-container *ngIf="user$ | async as user">
<p class="d-inline-block">{{user.orgUnit?.name}} </p>
<a href="#" class="d-inline-block">
({{user.orgUnit?.uid}})
</a>
<p>{{ user.name }}</p>
</ng-container>
结果:
我想在 header 中显示当前的 B2B 单位名称。 为当前客户检索 B2B 单元的正确方法是什么?
到目前为止,我发现/current 有一个负载?在初始页面加载时加载的端点,它包含 orgUnit:
当我尝试通过 CurrentUnitService 检索单位时,我得到了空值。
unit.component.ts:
orgUnit$: Observable<B2BUnit> = this.currentUnitService.item$;
constructor(
protected currentUnitService: CurrentUnitService,
) {}
unit.component.html:
<ng-container *ngIf="orgUnit$ | async as unit">
<p class="d-inline-block">{{unit.name}} </p>
<a href="#" class="d-inline-block">
({{unit.uid}})
</a>
</ng-container>
我能够使用 UserAccountFacade 并通过使用 orgUnit 扩展用户模型来检索当前客户的 B2BUnit,如下所示:
unit.component.ts
export class UnitComponent implements OnInit {
user$: Observable<CustomUser | undefined>;
constructor(
protected auth: AuthService,
protected userAccount: UserAccountFacade
) {}
ngOnInit(): void {
this.user$ = this.auth.isUserLoggedIn().pipe(
switchMap(isUserLoggedIn => {
if (isUserLoggedIn) {
return this.userAccount.get();
} else {
return of(undefined);
}
})
);
}
}
interface CustomUser extends User {
orgUnit?: B2BUnit
}
unit.component.html
<ng-container *ngIf="user$ | async as user">
<p class="d-inline-block">{{user.orgUnit?.name}} </p>
<a href="#" class="d-inline-block">
({{user.orgUnit?.uid}})
</a>
<p>{{ user.name }}</p>
</ng-container>
结果: