从一个.ts文件到另一个文件的简单模块导入

问题描述 投票:0回答:1

我有问题从一个.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

谢谢!

angular typescript ecmascript-6 protractor commonjs
1个回答
0
投票

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

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.