好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

【typescript】typescript的命名空间和模块的区别

一、命名空间

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

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

IShape.ts 文件代码:

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

Circle.ts 文件中导入IShape.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  " );

import { ZipCodeValidator }  from   "  ./ZipCodeValidator  " ;

示例:

IShape.ts 文件代码:

export  interface   IShape { 
   draw(); 
} 

Circle.ts 文件中导入IShape.ts 文件:

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

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

 

 转自: https://blog.csdn.net/weixin_43168278/article/details/105222589

查看更多关于【typescript】typescript的命名空间和模块的区别的详细内容...

  阅读:56次