为可观察类的属性分配新价值

发布于 2025-02-07 20:08:07 字数 1634 浏览 2 评论 0 原文

更新可观察值时,我有问题。我想使用此类创建一个过滤器:

export class TrackParams {
    sortBy: string = "";
    genreId?: number = null;
    minPrice = 0;
    maxPrice = 999.9;
    keyId?: number = null;
    minBpm = 0;
    maxBpm = 250;
    search?: string;
    isDraft: boolean = false;

    public constructor(init?:Partial<TrackParams>) {
        Object.assign(this, init);
    }
}

trackParams 获取任何参数,并将其仅分配给1个创建的对象。因此,我创建了一个仅与1个对象一起使用的服务,并具有用于过滤的参数。

export class TrackParamsService {
  trackParamsSource = new BehaviorSubject<TrackParams>(new TrackParams);
  trackParams = this.trackParamsSource.asObservable();
  constructor() { }

  getTrackParams() {
    return this.trackParams;
  }

  setTrackParams(value: TrackParams) {
    this.trackParamsSource.next(value);
  }
}

问题在于,我有多个独立组件来更改 trackParams 的属性值,并且我只能创建一个新的对象的 trackParams ,并覆盖具有默认值的其他属性。 例如,要更改 genreid ,我在下拉菜单中具有类型的组件,然后单击可与 trackparams genreid

constructor(private trackParams: TrackParamsService, private trackService: TrackService) { }

onClick(checked, genreId) {
    if (checked) {
      // Failed attemp
      // this.trackParams.trackParamsSource.pipe(
      //   map(tp => {
      //     tp.genreId = id}
      //     )
      // ).subscribe((tp) => {});
      this.trackParams.getTrackParams().subscribe(tp => {
        tp.genreId = id;
      });
    }
  }

我不想创建新的 : > TrackParams 因为它会弄乱我的 trackService 我使用 trackParam 属性值显示轨道。做什么正确的方法是什么?

I got a problem when updating observable values. I want to create a filter using this class:

export class TrackParams {
    sortBy: string = "";
    genreId?: number = null;
    minPrice = 0;
    maxPrice = 999.9;
    keyId?: number = null;
    minBpm = 0;
    maxBpm = 250;
    search?: string;
    isDraft: boolean = false;

    public constructor(init?:Partial<TrackParams>) {
        Object.assign(this, init);
    }
}

TrackParams takes any param and assigns it to only 1 created object. So I created service to work with only 1 object with params for filtering.

export class TrackParamsService {
  trackParamsSource = new BehaviorSubject<TrackParams>(new TrackParams);
  trackParams = this.trackParamsSource.asObservable();
  constructor() { }

  getTrackParams() {
    return this.trackParams;
  }

  setTrackParams(value: TrackParams) {
    this.trackParamsSource.next(value);
  }
}

The problem is that I have multiple independent components for changing property value of trackParams and I can only create a new object of TrackParams and override other properties with default value.
For example to change genreId, I have a component with Genres in a dropdown menu and on click that works with TrackParams genreId:

constructor(private trackParams: TrackParamsService, private trackService: TrackService) { }

onClick(checked, genreId) {
    if (checked) {
      // Failed attemp
      // this.trackParams.trackParamsSource.pipe(
      //   map(tp => {
      //     tp.genreId = id}
      //     )
      // ).subscribe((tp) => {});
      this.trackParams.getTrackParams().subscribe(tp => {
        tp.genreId = id;
      });
    }
  }

I don't want to create a new TrackParams because it messes up my trackService where I display the tracks using TrackParam property values to filter. What's the proper way of doing it?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫瑟鸿黎 2025-02-14 20:08:07

您可以让您的服务接受带有部分过滤器的更新调用:

export class TrackParamsService {
  trackParams$ = new BehaviorSubject<TrackParams>(new TrackParams);

  constructor() { }


  updateParams(newFilter: Partial<TrackParams>) {
    this.trackParams$.pipe(take(1).subscribe(currentVal => {
      this.trackParams$.next({...currentVal, ...newFilter})
    });
  }
}

然后从您的组件中订阅trackParams $(无需调用 asobservable()):(

this.trackParamsService.trackParams$.subscribe(filter =>{
 // do something with the updated filter
}}

不要忘记避免避免

要更新任何组件的过滤器,您可以使用部分属性调用服务:

onClick(checked, genreId) {
  if (!checked) return;

  this.trackParamsService.updateParams({ genreId }}
}

you could have your service accept an update call with a partial of the filter:

export class TrackParamsService {
  trackParams$ = new BehaviorSubject<TrackParams>(new TrackParams);

  constructor() { }


  updateParams(newFilter: Partial<TrackParams>) {
    this.trackParams$.pipe(take(1).subscribe(currentVal => {
      this.trackParams$.next({...currentVal, ...newFilter})
    });
  }
}

then from your components you subscribe to the trackParams$ (no need to call asObservable()):

this.trackParamsService.trackParams$.subscribe(filter =>{
 // do something with the updated filter
}}

(don't forget to avoid the memory-leak trap with subscriptions)

To update the filter from any component, you call the service with your partial attributes:

onClick(checked, genreId) {
  if (!checked) return;

  this.trackParamsService.updateParams({ genreId }}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文