如何与主体中的受试者正确刷新变型列表

发布于 2025-02-04 21:07:32 字数 2382 浏览 2 评论 0原文

我有两个列表可以刷新两个不同的事件(一个帖子,一个删除)。问题是,当调用删除时,它也称为邮政方法。因此,我删除了某个问题,但是帖子重新发布了:p

刷新列表是profileSer $和ptofilelistcistinct $

初始化:

export class UserEditComponent implements OnInit {

  userId: string = "";
  currentUser$!: Observable<User>;

  profileUser$!: Observable<ProfileUser[]>;
  profileListDistinc$!: Observable<Profile[]>;

  constructor(
    private activatedRoute: ActivatedRoute,
    private router: Router,
    private userService: UserService,
    private profileService: ProfileService,
    private reloadProfile: SearchEmitterService,
    private confirmDialogService: ConfirmDialogService
  ) { }

    ngOnInit(): void {
    
    const id = this.activatedRoute.snapshot.paramMap.get('id');
    this.userId = id ? id : "";

    this.currentUser$ = this.userService.getUser(this.userId);

    this.profileUser$ = this.reloadProfile.reloadprofileUserBehavior$.pipe(
      switchMap(
        x => this.profileService.getProfilesForUser(this.userId)));

    this.profileListDistinc$ = this.reloadProfile.reloadProfileListDisctincBehavior$.pipe(
      switchMap(x => this.profileService.getProfilesDisctinc(this.userId)));
  }

post方法:

  onAddProfileUser(profileId: string, userId: string) {
    let newProfileUser: ProfileUserCreate = {
      profileId: profileId,
      userId: userId
    }

    this.profileService.addProfileForUser(newProfileUser)
      .subscribe(x => {
        this.reloadProfile.reloadprofileUserBehavior$.next(null);
        this.reloadProfile.reloadProfileListDisctincBehavior$.next(null);
      });
  }

delete方法:

onDeleteProfileUser(profileUser: ProfileUser): void {
        this.profileService.deleteProfileForUser(profileUser)
          .subscribe(x => {
            this.reloadProfile.reloadProfileListDisctincBehavior$.next(null);
            this.reloadProfile.reloadprofileUserBehavior$.next(null);
          });
      }

重新加载服务:

export class SearchEmitterService {
  reloadProfileListDisctincBehavior$: BehaviorSubject<any> = new BehaviorSubject<any>(null);
  reloadprofileUserBehavior$: BehaviorSubject<any> = new BehaviorSubject<any>(null);

  constructor() { }

I have two list to refresh for two different event (one post, one delete). The issue is that when delete is called, It calls also the post method. So I delete someting, but the post re-post it :p

The lists to refresh are the profileUser$ and the ptofileListDistinct$

Initialization:

export class UserEditComponent implements OnInit {

  userId: string = "";
  currentUser$!: Observable<User>;

  profileUser$!: Observable<ProfileUser[]>;
  profileListDistinc$!: Observable<Profile[]>;

  constructor(
    private activatedRoute: ActivatedRoute,
    private router: Router,
    private userService: UserService,
    private profileService: ProfileService,
    private reloadProfile: SearchEmitterService,
    private confirmDialogService: ConfirmDialogService
  ) { }

    ngOnInit(): void {
    
    const id = this.activatedRoute.snapshot.paramMap.get('id');
    this.userId = id ? id : "";

    this.currentUser$ = this.userService.getUser(this.userId);

    this.profileUser$ = this.reloadProfile.reloadprofileUserBehavior$.pipe(
      switchMap(
        x => this.profileService.getProfilesForUser(this.userId)));

    this.profileListDistinc$ = this.reloadProfile.reloadProfileListDisctincBehavior$.pipe(
      switchMap(x => this.profileService.getProfilesDisctinc(this.userId)));
  }

Post Method:

  onAddProfileUser(profileId: string, userId: string) {
    let newProfileUser: ProfileUserCreate = {
      profileId: profileId,
      userId: userId
    }

    this.profileService.addProfileForUser(newProfileUser)
      .subscribe(x => {
        this.reloadProfile.reloadprofileUserBehavior$.next(null);
        this.reloadProfile.reloadProfileListDisctincBehavior$.next(null);
      });
  }

Delete Method:

onDeleteProfileUser(profileUser: ProfileUser): void {
        this.profileService.deleteProfileForUser(profileUser)
          .subscribe(x => {
            this.reloadProfile.reloadProfileListDisctincBehavior$.next(null);
            this.reloadProfile.reloadprofileUserBehavior$.next(null);
          });
      }

Reload Service:

export class SearchEmitterService {
  reloadProfileListDisctincBehavior$: BehaviorSubject<any> = new BehaviorSubject<any>(null);
  reloadprofileUserBehavior$: BehaviorSubject<any> = new BehaviorSubject<any>(null);

  constructor() { }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文