如何在角度中使用 api 数据调用 es6 类方法

问题描述 投票:0回答:0

在一个角度应用程序中,我有一个带有类和接口的产品模型,在类中我有一个计算后返回售价的方法。我调用了一个 api 并返回所有产品,我如何显示所有产品的售价使用 getsellingPrice() 方法的 html。

用户模型 `导出接口产品{ 编号:字符串; 标题:字符串; 价格:数量; 折扣:数量; getsellingPrice(): 数字; }

export class Product implements IProduct {
constructor(public id: string,public title: string, public price: number, public discount:     number) {
}

getsellingPrice(): number {
    let sellingPrice = this.price - (this.discount / 100 * this.price);
    return sellingPrice;
}
}`

用户服务 `_url:string = "http://localhost:3000"

constructor(private http:HttpClient) { }

getProducts():Observable<IProducts[]>{
    return this.http.get<IProducts[]>(`${this._url}/users`);
}`

组件

products!:IProduct[]; sellingprice:any; ngOnInit(): void { this.productService.getProducts().subscribe( (res) => { this.products = res; } ) }

我可以使用 new Product() 来做吗?

javascript angular es6-class
© www.soinside.com 2019 - 2024. All rights reserved.