在打字稿项目中,如何摆脱模块路径前面的点?
我尝试定义一个声明文件并将其导入,就像我们导入外部包的方式一样,但它抱怨Cannot find module 'module-name'
。
从一个文件导出所有模块并使用该文件时,我仍然需要导入我不喜欢的那个文件的相对路径。
例如,在我的测试套件中,我有:
import { Injector } from '../../../../src/lib/injector';
import { APIRequestManager } from '../../../../src/lib/request-manager';
import { DatabasePopulator } from '../../../../src/lib/database-populator';
import { DatabaseManager } from '../../../../src/lib/database-manager';
const databasePopulator = Injector.request<DatabasePopulator> ('DatabasePopulator');
const apiRequest = Injector.request<APIRequestManager>('APIRequestManager');
const databaseManager = Injector.request<DatabaseManager>('DatabaseManager');
因此,一种解决方案是从一个文件导出所有库:
的src / exporter.ts
export * from './lib/database-manager';
export * from './lib/database-populator';
export * from './lib/injector';
export * from './lib/request-manager';
我还需要在路径中使用尾随点导入:
import * as testHelpers from '../../../../src/exporter';
const databasePopulator = testHelpers.Injector.request<testHelpers.DatabasePopulator>('DatabasePopulator');
const apiRequest = testHelpers.Injector.request<testHelpers.APIRequestManager>('APIRequestManager');
const databaseManager = testHelpers.Injector.request<testHelpers.DatabaseManager>('DatabaseManager');
我希望能够做到这样的事情:
import * as testHelpers from 'testFramework';
const databasePopulator = testHelpers.Injector.request<testHelpers.DatabasePopulator>('DatabasePopulator');
const apiRequest = testHelpers.Injector.request<testHelpers.APIRequestManager>('APIRequestManager');
const databaseManager = testHelpers.Injector.request<testHelpers.DatabaseManager>('DatabaseManager');