gpt4 book ai didi

Angular 7 行为主体与类对象

转载 作者:行者123 更新时间:2023-12-02 18:10:46 24 4
gpt4 key购买 nike

我有以下类(class)

export class filter {
public PageRecords: number;
public SearchText: string;
public SortColumn: string = null;
public SortDirection: string = null;
public StartingPos: number;
}

以下行为主题

filterParam = new BehaviorSubject(new filter);

更新主题

this.filterParam.next(<filter>{StartingPos  : 1,PageRecords : 10 })

当我得到主题的值时

this.filterParam.value

它只有两个属性 GettingPos 和 PageRecords 我更新了。它失去了其他 Prop 。

如何解决这个问题?

最佳答案

这是因为您没有直接传递 Filter 对象类。您正在将一些动态 json 转换为过滤器对象,但仅具有这两个属性。

你可以这样做:

const filterParam = new filter();
filterParam.StartingPos = 1;
filterParam.PageRecords = 10;

this.filterParam.next(filterParam);

编辑:我没有注意到您只想更新BehaviorSubject 值中的两个值。正如 @ritaj 在这个问题下面的评论中建议的那样,你可以做他建议的事情:

this.filterParam.next(Object.assign(this.filterParam.value, {StartingPos: 1, PageRecords: 10 }

Object.assign() copies the values (of all enumerable own properties) from one or more source objects to a target object. It has a signature of Object.assign(target, ...sources). The target object is the first parameter and is also used as the return value. Object.assign() is useful for merging objects or cloning them shallowly.

关于Angular 7 行为主体与类对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54475520/

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