一、命名空间

如果一个命名空间在一个单独的 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)"); 
   } 
}

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

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

 

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

相关文章:

  • 2022-01-10
  • 2021-07-25
  • 2022-12-23
  • 2022-01-02
  • 2021-07-31
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-02-28
  • 2022-01-21
  • 2021-07-15
  • 2022-12-23
相关资源
相似解决方案