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

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;
}
}
主站蜘蛛池模板: 潜山县| 盘山县| 密山市| 宁南县| 南丰县| 驻马店市| 海盐县| 武宁县| 英吉沙县| 清新县| 肃宁县| 辉县市| 平阴县| 富源县| 肇源县| 滨海县| 新密市| 保德县| 芒康县| 宜章县| 苏尼特左旗| 廊坊市| 江山市| 黎川县| 缙云县| 安溪县| 吉隆县| 潮州市| 平远县| 南乐县| 射阳县| 共和县| 绿春县| 江津市| 拉孜县| 措勤县| 荣成市| 通榆县| 临朐县| 麻城市| 安徽省|