Typescript import using a path from some root

Is there any easy way to configure so that I can importstart from some root point?

eg:.

- module1
   \- file1.ts
- module2
   \- file2.ts

in file2 i want importfile1

import { foo } from '../module1/file1'

Instead, I would like to do the following:

import { foo } from 'src/module1/file1'

Angular2 somehow uses this (see https://github.com/angular/angular/blob/master/modules/angular2/src/core/compiler/compiler.ts ). I looked into their build process, but it's so complicated, I'm not sure how they included it.

+4
source share
3 answers

ES6 , angular2 systemjs / . systemjs . ( )

, ES6 javascript, , src , /src/module1/files

+1

https://www.npmjs.com/package/app-module-path node.

:

root
  \- module1
  |  \- file1.ts
  \- module2
  |  \- file2.ts
  \- main.ts

main.ts , :

import * as path from 'path';

require('app-module-path').addPath(path.resolve(__dirname, '..'));

module2/file2.ts :

import { Stuff } from 'root/module1/file1';
+1

Angular CLI , .

, , , src/app.

HTTP src/app/services/httpService.ts.

, :

import { HttpService } from 'app/services/httpService'

I'm not sure if this depends on any special configuration that the Angular CLI does (and I understand that the Angular CLI was not there at the time this question was posted), but it's worth a try in your environment.

+1
source

All Articles