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

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;
}
}
主站蜘蛛池模板: 遵义县| 台南县| 临漳县| 鸡东县| 新绛县| 上杭县| 兰溪市| 邹城市| 武隆县| 文山县| 五指山市| 西乡县| 兴仁县| 海伦市| 乐都县| 邹城市| 嘉荫县| 百色市| 板桥市| 资源县| 哈尔滨市| 巴里| 友谊县| 元江| 奉节县| 达孜县| 永兴县| 西和县| 云南省| 永川市| 泰和县| 海城市| 云安县| 合阳县| 霸州市| 大庆市| 鄂托克前旗| 聊城市| 佛冈县| 昭觉县| 龙里县|