Angular CLI项目中的绝对路径 [英] Absolute path in Angular cli project
问题描述
如何在cli生成的角度项目中使用绝对路径?
How can I use absolute path in an angular cli generated project?
所以我有这个路径:src -> app -> shared
我想写import {} from 'shared/ffdsdf/my.module.ts'
而不是'../shared/ffdsdf/my.module.ts'
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'
推荐答案
有一个TypeScript功能允许这样做.
There is a TypeScript feature that allows this.
您可以修改您的src/tsconfig.json
文件以启用此功能,在compilerOptions
下,添加以下内容:
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.
您还可以选择前缀而不只是*
(特别是在遇到问题的情况下),可以使用~/*
之类的东西,然后您的导入将全部为from '~/shared/sample'
等.
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屋!