我有这样一个目录结构:
app
|- core
| |_identity
| | |_identity.service.ts
| |_ http
| |_apiClient.service.ts (service for interacting with my api)
|-user
| |- models
| | |-user.ts
| |- services
| | | - user.service.ts
| |-listComponent
|
|-tenant
|-models
| |-tenant.ts
|- services
|- tenant.service.ts
....我的tenant.service和user.service都注入了我的ApiClient对象(扩展了http服务)。问题是,要引用它,我必须补充
import {ApiClient} from '../../../core/http/apiClient.service'
在我所有想注入它的组件中。我已经定义了一个core.module (并导入到我的app.module中),并且希望能够引用我的ApiClient,而不必知道目录结构(也就是说,我希望能够像使用import {ApiClient} from '@core/http'的http服务那样导入它。
发布于 2017-06-20 20:26:13
这就是模块解析在打字稿中的工作方式。
TypeScript将模仿Node.js运行时解析策略,以便在编译时定位模块的定义文件。
阅读更多关于它的这里。
您可以为您的公共功能创建一个角库,并使用package.json安装它,以便与其他库一样,公共模块位于node_modules文件夹中。然后,您可以很容易地使用所有导出的类型,而不知道目录路径,
import { ApiClient } from 'MyLib';希望这能帮上忙!
https://stackoverflow.com/questions/44662246
复制相似问题