首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rxjs在behaviorsubject中包含深度嵌套的对象

Rxjs在behaviorsubject中包含深度嵌套的对象
EN

Stack Overflow用户
提问于 2016-08-15 21:12:48
回答 1查看 997关注 0票数 1

我开始在Rxjs中使用BehaviorSubject,但在这方面的经验有限。到目前为止,我能够获得组件中的根级参数,但访问嵌套对象会导致“无法读取未定义的属性x”。

类:

代码语言:javascript
复制
export class Basket extends X23Object {
    public BasketId: number;
    public UserID: number;
    public User: User;
    public BasketLines: BasketLine[] = [];
}

export class BasketLine extends X23Object {
    public BasketLineId: number;
    public BasketId: number;
    public ProductId: number;
    public Quantity: number;
    public Product: Product;
    public Price: number;
    public Name: string;
}
export class Product extends X23Object {
    public ProductId: number;
    public CategoryId: number;
    public Name: string;
    public Code: string;
    public Price: number;
    public Details: string;
    public Images: Image[];
    public Category: Category;
}

BasketBackendService

代码语言:javascript
复制
GetBasket() {
    return this.authHttp.get(this.apiUrl + 'api/GetBasketByUser?id=' + parseInt(this.decodedJwt['userId']), { headers: contentHeaders });
}

BasketService

代码语言:javascript
复制
private _lines: BehaviorSubject<BasketLine[]> = new BehaviorSubject([new BasketLine]);
get getLines() {
    return this._lines.asObservable();
}
loadBasket() {
    this.basketBackendService.GetBasket()
        .subscribe(
            response => {
                let lines = <BasketLine[]>response.json().basket.BasketLines;

                this._lines.next(lines);
            },
            error => console.log(error.text())
        );
}

模板(代码段)

代码语言:javascript
复制
<tr *ngFor="let line of basketService.getLines | async">
    <td><img class="login-logo" src="{{ line.Product.Images[0].ThumbUrl }}" width="100%" /></td>
    <td><a [routerLink]="['Product', { id: line.ProductId }]"> {{ line.Product.Code }} </a></td>
    <td><a [routerLink]="['Product', { id: line.ProductId }]">{{ line.Product.Name }}</a></td>
    <td class="text-right">{{ line.Price | currency:'GBP':true:'.2-2' }}</td>
    <td class="text-center">{{ line.Quantity }}</td>
    <td class="text-right">{{ line.Quantity * line.Price | currency:'GBP':true:'.2-2' }}</td>
    <td><button (click)="DeleteLine(line.BasketLineId)">Remove</button></td>
</tr>

如果我删除深度嵌套对象的引用,我会得到预期的返回结果。

我正在尝试使用BehaviorSubject来更新几个组件,但不确定这是否是最好的解决方案!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-16 00:01:48

代码在我看来很好,我猜“深度嵌套”指的是例如line.Product.Code。如果它适用于line.Quantity,那么问题很可能不在于BehaviorSubject,而在于您的数据结构。

我不知道您的特定用例是什么,但您根本不需要使用BehaviorSubjectasync管道。

对于BasketService,您可以只使用:

代码语言:javascript
复制
export class BasketService {
    lines: BasketLine[];

    // ...

    loadBasket() {
        this.basketBackendService.GetBasket().subscribe(
            response => {
                this.lines = <BasketLine[]>response.json().basket.BasketLines;
            },
            error => console.log(error.text())
        );
    }
}

然后使用以下命令进行渲染:

代码语言:javascript
复制
<tr *ngFor="let line of basketService.lines">
    // ...
</tr>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38955755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档