Absolute path in Angular cli project
better sample:
tsconfig.json
{
"compilerOptions": {
"baseUrl": "src",
"paths": {
"@app/*": [
"app/*"
],
"@app/core/*": [
"app/core/*"
],
"@app/shared/*": [
"app/shared/*"
],
"@env/*": [
"environments/*"
]
}
}
}
use:
import { someService } from "@app/core/some.service";
instead of:
import { someService } from "./../../core/some.service";
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.