angular - 在 Angular CLI 中避免相对路径

标签 angular typescript webpack angular-cli

我使用的是最新的 Angular CLI,我创建了一个自定义组件文件夹,它是所有组件的集合。

例如,TextInputComponent 有一个 TextInputConfiguration 类,它位于 src/components/configurations.ts 中,在 src 中/app/home/addnewuser/add.user.component.ts 我用它的地方是:

import {TextInputConfiguration} from "../../../components/configurations";

这很好,但是随着我的应用变得越来越大和深入,../ 增加了,我该如何处理这个问题?

以前,对于 SystemJS,我会通过 system.config.js 配置路径,如下所示:

System.config({
..
 map : {'ng_custom_widgets':'components' },
 packages : {'ng_custom_widgets':{main:'configurations.ts', defaultExtension: 'ts'},
)};

如何使用 Angular CLI 为 webpack 生成相同的内容?

最佳答案

根据 this comment ,您可以通过 tsconfig.json 中的 paths 添加您的应用程序源:

{
  "compilerOptions": {
    ...,  
    "baseUrl": ".",
    "paths": {
      ...,
      "@app/*": ["app/*"],
      "@components/*": ["components/*"]
    }
  }
}

然后你可以绝对地从 app/components/ 导入而不是相对于当前文件:

import {TextInputConfiguration} from "@components/configurations";

注意:如果 paths 是,则必须指定 baseUrl

另见

关于angular - 在 Angular CLI 中避免相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41460810/

相关文章:

Angular 构建错误 "..has no exported member ' ɵReflectorReader'。”

javascript - token 刷新后, Angular http拦截器不再调用请求

javascript - 如何访问 Angular2 组件中的 codemirror 文本区域值?

reactjs - 无法让 Typescript 理解 Material-UI 组件上的 css Prop (情感)

webpack - 我可以使用 webpack 捆绑 Next.js 自定义服务器吗?

javascript - typescript 编译 vendor ,排除不起作用

javascript - 如何更改有关 if else 语句的 Angular fxLayout(行到列)

angular - HTTP拦截器发送相同的请求两次

javascript - 函数式编程中的nodejs createReadStream

reactjs - React 输出目标的 StencilJS 错误 - 您可能需要额外的加载器来处理这些加载器的结果