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

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;
}
}
主站蜘蛛池模板: 新化县| 镇原县| 临邑县| 正蓝旗| 沙湾县| 湘阴县| 连州市| 威宁| 河津市| 茂名市| 扎囊县| 龙泉市| 通山县| 砀山县| 逊克县| 确山县| 永嘉县| 大兴区| 乐安县| 金川县| 西华县| 左权县| 广东省| 高台县| 惠水县| 黎城县| 岑巩县| 准格尔旗| 陕西省| 宣恩县| 盈江县| 新野县| 蓬溪县| 龙山县| 林州市| 汉川市| 仙桃市| 宜兴市| 桐庐县| 马尔康县| 水城县|