简体   繁体   English

目录中所有文件的TsConfig.json路径匹配

[英]TsConfig.json path matching for all files in directory

I have been looking into tsconfig.json and found that it can have paths created. 我一直在研究tsconfig.json ,发现它可以创建路径。 Currently the only way I can find that works is something like this: 目前,我能找到起作用的唯一方法是这样的:

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

Which then allows me to do something like this: 然后,我可以执行以下操作:

import { dialogSale } from '@components/dialog-sale/dialog-sale.component';

This works fine however for my folder structure is a bit of a waist of time as my base path is already at app so I only have to do this which is actually 1 character shorter: 这很好用,但是因为我的文件夹结构已经花了很多时间,因为我的基本路径已经在应用程序上了,所以我只需要这样做,实际上要短1个字符:

import { dialogSale } from 'components/dialog-sale/dialog-sale.component';

What I would like to do to shorten my import statements as well as to reduce developer error with sub directories is to be able to use like this: 我想要做的是缩短导入语句以及减少子目录的开发人员错误,就是要像这样使用:

import { dialogSale } from '@components/dialog-sale.component';

Above would be the most ideal as it reduces the chance of developer error as well as straight and to the point for import statements. 上面是最理想的选择,因为它减少了开发人员出错的可能性,并减少了导入语句的难度。

If you want it shorter you should further specify your paths. 如果要缩短它,则应进一步指定路径。 For example: 例如:

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

and then you could use: 然后您可以使用:

import { dialogSale } from '@components/dialog-sale.component';

This would not be really practical if you later want to add other 'components' different from dialog-sale.. In that case you could do: 如果您以后要添加其他与对话框销售不同的“组件”,那么这将不切实际。

"compilerOptions": {
  "baseUrl": "./src/app",
  "paths": {
    "@dialogSale": ["components/dialog-sale/*"]
 }
}

and use: 并使用:

import { dialogSale } from '@dialogSale/dialog-sale.component';

Depending on how it works best for you. 取决于最适合您的方式。 Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM