FullStackDeveloper
FullStackDeveloper

Reputation: 968

How to specify the path when importing service into service and component?

I know in my service and component I need to use import {MyService} from '../my-service' if I want to import a service.

But my question is:

Is there a quick way to specify the path where my service is? Sometimes I use '../', sometimes I use './'. Sometimes I have to use '../parentFolderOfMyService', sometimes I have to use '../parentFolderOfMyService/my-service'.

P.S. I am using visual studio 2017.

Upvotes: 0

Views: 451

Answers (2)

k11k2
k11k2

Reputation: 2044

It's not about Service or component , all about folder/files hierarchy.

when we will use ../../something1/something2/service is when our would be like this. importing service.ts in our service.component.ts

app
   |something1
             |something2
                        -(service.ts)
                        -(service.html)
                          -service.component.ts

for more

src
  |app
      |folder1
              (.ts file)
      |folder2
              (.ts file1)
              (.ts file2)
  |app.module.ts

If I'm importing folder1 .ts file in folder2 .ts file

import {filecomponent} from 'app/folder1/file.component'

Importing folder2 file1.ts file in folder2 file2.ts file

import {file1component} from './file1.component'

Importing folder1 .ts file in app.module.ts

import {filecomponent} from './folder1/file.component'

Upvotes: 0

Ignasi
Ignasi

Reputation: 731

./ refers to the current directory (the directory of the file you are writting)

../ refers to the parent directory

The names are just the names of the directories

To avoid having to write the name of the file you can use barrels

Upvotes: 1

Related Questions