1
votes

I have created a component called Parent and Child. I want to display all the UI of ChildComponent to my ParentComponent.

child.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-child',
  templateUrl: './child.component.html',
  styleUrls: ['./child.component.scss']
})
export class ChildComponent implements OnInit {

  testContent = 'child component content...';

  constructor() { }

  ngOnInit() {
  }

}

child.component.html

<p>{{testContent}}</p>

parent.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-admin',
  templateUrl: './admin.component.html',
  styleUrls: ['./admin.component.scss'],
  directives: [ChildComponent]
})
export class AdminComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

parent.component.html

<div>
  Lorem ipsum
  <app-child></app-child>
</div>

I want to display the contents of child component inside parent component. However, I am encountering an error in Angular 4

"Object literal may only specify known properties, and 'directives' does not exist in type 'Component'."

Do you have any idea what is the alternative property to add child component?

2

2 Answers

1
votes

Remove directives from the parent component and add the child component to the module declarations array where the parent component lives.

0
votes

Directives and pipes in @component are deprecated since angular RC6. Just remove it from the component.

@Component({
  selector: 'app-admin',
  templateUrl: './admin.component.html',
  styleUrls: ['./admin.component.scss']
})