Angular cli 项目中的绝对路径 [英] Absolute path in Angular cli project

查看:24
本文介绍了Angular cli 项目中的绝对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 angular cli 生成的项目中使用绝对路径?

所以我有这个路径:src ->应用程序->分享我想写 import {} from 'shared/ffdsdf/my.module.ts'而不是 '../shared/ffdsdf/my.module.ts'

解决方案

TypeScript 功能允许这样做.

您可以修改您的 src/tsconfig.json 文件以启用此功能,在 compilerOptions 下,添加以下内容:

<前>{编译器选项":{//...路径":{*":["./*",应用程序/*",../node_modules/*"]}}

您显然可以根据需要更改模式键和值.您添加或删除文件夹,您可以更改顺序等.

你也可以选择一个前缀而不是 *(特别是如果它出现问题),你可以使用类似 ~/* 的东西,然后你的导入将是所有来自'~/shared/sample'

How can I use absolute path in an angular cli generated project?

So I have this path : src -> app -> shared and I would like to write import {} from 'shared/ffdsdf/my.module.ts' instead of '../shared/ffdsdf/my.module.ts'

解决方案

There is a TypeScript feature that allows this.

You can modify your src/tsconfig.json file to enable this, under compilerOptions, add the following:

{
  "compilerOptions": {
    // ...
    "paths": {
      "*": [
        "./*",
        "app/*",
        "../node_modules/*"
      ]
    }
}

You can obviously change the pattern key and values as needed. You add or remove folders, you can change the order, etc.

You can also choose a prefix instead of just * (especially if it cases problems), you can use something like ~/*, and your imports will then be all from '~/shared/sample' etc.

这篇关于Angular cli 项目中的绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆