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

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;
}
}
主站蜘蛛池模板: 阿克苏市| 甘南县| 新河县| 明星| 会同县| 姚安县| 博客| 长岭县| 宿州市| 陆河县| 呼图壁县| 綦江县| 昭觉县| 彭州市| 类乌齐县| 定州市| 宁强县| 贺兰县| 堆龙德庆县| 青河县| 武山县| 望谟县| 嘉兴市| 房产| 上蔡县| 桓仁| 视频| 舞钢市| 抚顺市| 雅江县| 璧山县| 三都| 资溪县| 苍溪县| 云林县| 浦城县| 武山县| 裕民县| 平凉市| 宁蒗| 长白|