I'm using the Angular CLI. How do I convert a path from:
import {AppConfig, AppConfigInterface} from '../../../app.config';
to something like:
import {AppConfig, AppConfigInterface} from 'root/app.config';
Typescript aliases allow you to specify a word/alias for an absolute path in the application from which you can resolve paths from absolutely. This means that you can have an alias called @app for the absolute path “src/app” and if you wanted to import a module from “src/app/config/config.
Path aliases simplify paths by giving a link to the path rather than using the the fully qualified path name. Using them will make your code easier to read and maintain. Path aliases are relative to compilerOptions.
So by the use of relative path you are making your links free that are relative to the current URL segment. Your feature area of routing will be the same even if you change the route for the parent. You are just making your link free even if you change the parent route path.
try this in tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2017",
"dom"
],
"paths": {
"@services/*": ["app/services/*"] // here!
}
}
}
I found this question when tried to resolve scss
. I found current approach will not work because of this issue
workaround is to add stylePreprocessorOptions
to .angular-cli.json
so it will resolve all styles within the directory
{
"apps": [{
...
"stylePreprocessorOptions": {
"includePaths": [
"./app/global-styles"
]
},
...
}]
}
for server-side rendering you will need to add this for both ssr
and main app build - otherwise you will get NodeInvocationException: Prerendering failed because of error: Error: Cannot find module 'C:\Users\...\ClientApp\dist-server\main.bundle.js'
{
"apps": [{
...
"outDir": "dist",
"stylePreprocessorOptions": {
"includePaths": [
"./app/global-styles"
]
},
...
},
{
...
"name": "ssr",
"outDir": "dist-server",
"stylePreprocessorOptions": {
"includePaths": [
"./app/global-styles"
]
},
...
}
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With