typescript的命名空间和模块 区别心得

如果一个命名空间在一个单独的 TypeScript 文件中,则应使用三斜杠 /// 引用它,语法格式如下:

/// <reference path = "SomeFileName.ts" />

IShape.ts 文件代码:

namespace Drawing {
    export interface IShape { draw(); } 
}

Circle.ts 文件代码:

/// <reference path = "IShape.ts" />
namespace Drawing { 
    export class Circle implements IShape { 
       public draw() { 
            console.log("Circle is drawn"); 
        }
     }
 }

模块导出使用关键字 export 关键字,语法格式如下:

// 文件名 : SomeInterface.ts 
export interface SomeInterface { 
   // 代码部分
}

要在另外一个文件使用该模块就需要使用 import 关键字来导入:

import someInterfaceRef = require("./SomeInterface");

IShape.ts 文件代码:

/// <reference path = "IShape.ts" /> 
export interface IShape { 
   draw(); 
}

Circle.ts 文件代码:

import shape = require("./IShape"); 
export class Circle implements shape.IShape { 
   public draw() { 
      console.log("Cirlce is drawn (external module)"); 
   } 
}

注意引用文件的代码,一个有格式后缀一个没有格式后缀:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值