从一个.ts文件到另一个

时间:2018-03-01 09:35:13

标签: angular typescript ecmascript-6 protractor commonjs

我在将类从.ts文件导入到另一个.ts文件时遇到问题。

这是我的项目结构:

enter image description here

我正在尝试将print.ts导入testing.ts。

这是我的tsconfig.json的样子:

enter image description here

我的测试内容。

import { names } from 'testing';
console.log(names.printFirstName());

我的print.ts内容:

class nameCollection {
static printFirstName(){
    return 'OYEAAAH';
    };
}

export {nameCollection as names};

我正在使用以下方式运行测试:     node testing.js

我得到了: enter image description here

谢谢!

1 个答案:

答案 0 :(得分:0)

Typescript为模块解析做路径映射,你正在混合目录和文件

{
  "compilerOptions": {
    "paths": {
      "testing/*": ["src/nested/*"]
    }
  }
}

然后在您的脚本中,您可以导入import {names} from 'testing/print';

检查https://www.typescriptlang.org/docs/handbook/module-resolution.html了解详情