Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an “alias” for a long path in angular-cli?

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';
like image 926
manidos Avatar asked Jan 06 '17 13:01

manidos


People also ask

How to use path alias Typescript?

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.

What is aliases in angular?

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.

What is relative path in angular?

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.


2 Answers

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!
    }
  }
}
like image 119
Xigua.liu Avatar answered Oct 12 '22 08:10

Xigua.liu


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"
                ]
            },
            ...
        }
    ]
}
like image 29
godblessstrawberry Avatar answered Oct 12 '22 09:10

godblessstrawberry