官术网_书友最值得收藏!

Importing

Importing allows us to import items from an exported module. We do this using an import statement that includes the item names to import in curly braces and the file path to get the items from (excluding the ts extension). We can only import items that are exported in the other module file.

  1. Let's resolve the issue with our OrderDetail class by importing the Product interface:
import { Product } from "./product";

class OrderDetail {
product: Product;
quantity: number;
getTotal(discount: number): number {
const priceWithoutDiscount = this.product.unitPrice * this.quantity;
const discountAmount = priceWithoutDiscount * discount;
return priceWithoutDiscount - discountAmount;
}
}
  1. We can rename imported items using the as keyword in an import statement. We then reference the item in our code using the new name:
import { Product as Stock } from "./product";

class OrderDetail {
product: Stock;
quantity: number;
getTotal(discount: number): number {
const priceWithoutDiscount = this.product.unitPrice * this.quantity;
const discountAmount = priceWithoutDiscount * discount;
return priceWithoutDiscount - discountAmount;
}
}
主站蜘蛛池模板: 名山县| 靖边县| 交城县| 巫溪县| 洛川县| 灵丘县| 吉安县| 体育| 长丰县| 闽清县| 永丰县| 奉化市| 乌海市| 甘泉县| 黎平县| 浦县| 湖口县| 锡林浩特市| 安远县| 高碑店市| 太白县| 禹州市| 谢通门县| 汝州市| 玉田县| 汉川市| 黔东| 罗源县| 札达县| 靖安县| 富顺县| 孟村| 宜春市| 郸城县| 咸阳市| 和硕县| 安泽县| 东阳市| 千阳县| 罗平县| 石泉县|