Importing a component from another component

Yozki picture Yozki · Feb 15, 2017 · Viewed 10.9k times · Source

I'm trying to call a child component's function from a parent component.

As I understand, the best way to go about this is to use ViewChild.

I understand the logic of it all, but for some reason I can't import the child component into the parent component, it gives me the error:

Cannot find module '../claim-header/claim-header.component'.

This is the import section in my parent component: (Claim)

import { ClaimHeaderComponent } from '../claim-header/claim-header.component';

I've tried them all:

import { ClaimHeaderComponent } from './claim-header';
import { ClaimHeaderComponent } from './claim-header/claim-header';
import { ClaimHeaderComponent } from '../claim-header';
import { ClaimHeaderComponent } from '../../components/claim-header-component';

etc.

For reference, the rest of the imports work fine as long as they're not components:

import { ClaimDetail } from '../../modules/models/claim';
import { ClaimsService } from '../../modules/services/claims.service';

This is the declaration of my child component:

@Component({
    moduleId: module.id,
    selector: 'fhp-claim-header',
    templateUrl: 'claim-header.component.html'
})

export class ClaimHeaderComponent implements OnInit {
   ...
}

And my folder layout:

Folder layout

Answer

Aravind picture Aravind · Feb 15, 2017

You should be using

../claim-header/claim-header.component

Explanation.

./ refers to the root folder of the underlying folder -> Components folder

enter image description here

My folder structure for your demo

enter image description here